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!





