This is my list of my most used chaining operators in Ubuntu.
1 . Semi-colon operator ;
The semi-colon allows you to chain multiple commands so that they run in-order.
# apt-get update ; apt-get upgrade ; echo 'upgrades are done'
2 . Single Ampersand &
The single ampersand will execute a command in the background and can chain commands to run in the background. You use the command followed by a space and the ampersand per command.
To run a single command:
# ping www.slsmk.com &
To run more than 1 command in the background:
# ping www.slsmk.com & cp ~/* . & apt-get update &
3 . Double Ampersand AND Operator &&
The && symbol, also called the AND Operator, links and executes commands in order only if the previous command is successful.
Technically a command is successful if it completes with exit status 0.
For example, I want to create a directory and file, but I only want to create the file is the directory is created correctly.
# mkdir ~/test && touch ~/test/tempfile1
4. Single PIPE |
The PIPE operator is used when you want the output of one command to be the input of a following command.
For example, this will list installed packaged then search for lines with ‘java’.
# dpkg -l | grep java
5. OR Operator ||
The OR operatorm || is similar to the AND operator, only here it will execute the following command only if the previous command failed. A command fails if it exits with status code 1.
# mkdir ~/test || echo 'The command failed'
6. NOT Operator !
The NOT operator ! is used in a command to identify those items that should be exempt from the command.
For example, imagine a directory with various filetypes and you wanted to remove all files except the PDFs.
# rm -r !(*.pdf)
7. AND OR Operator && ||
This combination of the AND && and the OR || operators delivers what is basically an if-else statement based on the exit status code of the 1st command. BASH Shell has other command to get an if-else result, but this is using just the Operators.
# mkdir ~/testdir && echo 'Directory Created' || echo 'Directory Creation Failed'
8. Precedence ()
When using && and ||, the exit codes determine whether or not to execute the following commands. Also, it is important to understand that the && and || only evaluate the 2 commands preceding and following the operators. So when using multiple operators, setting groups and precedence comes in handy when you want to ensure groups of commands complete or fail in a certain way.
In this example, Both commands in the 1st set () must exit with 0 in order for the next () set to execute.
# (ls *.pdf > pdf-files.list && cp *.pdf ~\) && (ls *.tar > tar-tiles.list && cp *.tar ~\) || echo 'Needs attention'
9. IF THEN ELSE
Precedence can become very unreadable very quickly. I prefer using BASH’s IF THEN ELSE commands. These work just like any programming languange… IF something is true, THEN execute this command, otherwise (ELSE) run this command. Note that in BASH the if is concluded with ‘fi’.
There are pages of options for IF THEN ELSE which you should explore.
For example, this is a very basic example:
# if ls *.pdf ; then echo 'There are PDFs here' ; else echo 'There are no PDFs here' ; fi