Logical operators
Let's now take a look at logical operators.
Command1 & command2
The first command is started in the background to continue until it has finished; immediately after starting the first command, the second command is started and it will run in the foreground:
$ find / -name "*.z" & ls---------------- -----Command1 command2
In the preceding example, the first command, find
, will start running in the background and, while the find
command is running in the background, the ls
command will start running in the foreground.
Command1 && command2
The second command is only started if the first command is successful. To achieve this, the shell checks the exit (return) status of the first command and starts the second command only if and when that exit status is found to be 0
:
$ ls /home/ganesh && echo "Command executed successfully"Since we are working as user ganesh,$ ls /root && echo "Command executed successfully"
Since we are working as...