Executing two or more commands in sequence - Why/When/How

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Habitual

Executing two or more commands in sequence - Why/When/How

Post by Habitual »

As you may or may not know every command exits with a status. An exit status of 0 is true, while everything else is false.
Additionally, the exit status is stored in the $? variable.

To execute two or more commands in sequence regardless of the failure/success of the previous command, you can use a single semi colon ";" in between of your commands. For example:

Code: Select all

$ mycommand1 ; mycommand2
In this example, mycommand2 will be executed after mycommand1 have been accomplished, successfully, or unsuccessfully.
you can test this with these 2 commands by using:

Code: Select all

false ; whoami 
true ; whoami
whoami executes in both examples regardless of the true|false command's exit status.

[Logical OR] OR logical operator
To execute the next command in condition of when the first command fails, you can use logical OR operator which is double pipes "||".

Code: Select all

$ mycommand1 || mycommand2
mycommand2 in this example will only be executed if mycommand1 failed.
you can test this with these 2 commands by using:

Code: Select all

false || whoami
true || whoami
[Logical AND]AND logical operator.
To execute the next command in condition of when the first command is successfully accomplished, you can use logical AND operator which is double ampersand "&&". Look at this example:

Code: Select all

$ mycommand1 && mycommand2 && myucommand
You typically see this when you have to install from source

Code: Select all

./configure && make && make install 
each successive command depends on the successful completion of the previous.

In this last example, mycommand2 will only be executed when mycommand1 is successful. etc..

I am not a programmer. I am a "Machete Coder" (Chop and Hack / Trial and Error methods)
So don't hold me responsible! As will all things Linux, verify it for yourself!

References:
Command Lists Logical Operators
Advanced Bash-Scripting Guide

Enjoy!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29588
Joined: Wed Jul 06, 2011 3:58 am

Re: Executing two or more commands in sequence - Why/When/Ho

Post by xenopeek »

Habitual wrote:[Logical And/Or] or is it [Logical Either/Or]? <--- Clarification needed here.

Code: Select all

$ mycommand1 ; mycommand2
There is nothing logical about the ; control operator :lol: It does sequential execution of each command. And don't forget about the & control operator, also useful here I think.

From the manpage:
If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.
Image
Habitual

Re: Executing two or more commands in sequence - Why/When/Ho

Post by Habitual »

Vincent:

Yes, I was confusing the Logic issue with Truth Tables.
Something I picked up in my software-cracking days with PhrozenCrew.

I have to (re)learn new stuff to push out the old stuff, I guess.
Arliegiles

Re: Executing two or more commands in sequence - Why/When/Ho

Post by Arliegiles »

We can execute multiple commands by using logical operator..
Locked

Return to “Scripts & Bash”