Pages

Friday 13 April 2012

Bash: List of commands (Executing Commands Sequentially)


The bash shell has an interesting feature where one is able to execute a list of commands sequentially. A list refers to a sequence of one or more commands that are separated by the following operators; '&', '&&', '||', ';', or the new line character.

Operator Meaning Example
&
Sends processes to the background. This effectively creates a sub-shell for each command that ends with the & operator. One can view the list of background processes by typing the jobs command Command1 & Command2 & Command3 &
&&
This operator represents the AND option. What is essentially means is that a command will execute if and only if the/a previous command returns with a zero exit status/return value. Command1 && Command2 && Command3
||
This operator represents the OR option. A command will only be executed by the bash shell if and only if the previous command returned with a non-zero exit status. Command1 || Command2 || Command3
;
The shell executes commands separately this operator sequentially. This means that the shell waits for the previous command to complete executing and return control to the shell that it executes the next shell. This command is similar to separating commands using the new line. Command1; Command2; Command3
\n
This command represents the new line command. It is similar to entering a first command followed by pressing the “Enter” or “Return” keyboard key. Command1 \n Command2 \n Command3