Logical AND operator(&&):
The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.
command1 && command2
command2 will execute if command1 has executed successfully. This operator allows us to check the exit status of command1.
It is used to execute multiple commands in one go. Several commands can be chained together using this operator. The execution of the command succeeding this operator will always execute after the command preceding it has executed irrespective of the exit status of the preceding command. The commands always execute sequentially. Commands separated by a semicolon operator 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.
command1 ; command2
The execution of the second command is independent of the exit status of the first command. If the first command does not get successfully executed, then also the second command will get executed.
Difference between AND(&&) and SEMI-COLON(;) operator:
&& (logical AND) operator
; (Semi-colon) operator
|The execution of the second command depends on the execution of the first command
||The execution of the second command is independent of the execution status of the first command.
|If the exit status of the preceding command is non-zero, the succeeding command will not be executed.
||Even If the exit status of the first command is non-zero, the second command will be executed.
|Allows conditional execution
||Does not allow conditional execution.
|Bash has short-circuited evaluation of logical AND.
||No short circuit evaluation is needed.
|Logical AND has higher precedence.
||It has lesser precedence than logical AND.
[ -z $b ] && b = 10
[ -z $b ] && b = 15
The first command checks if a variable b exists and if it exists, it is initialized with 10. Consider that b does not exist, so the first command has an exit status 0 and therefore the second command is executed and b is initialized with 10. Now, if we again try to initialize it using the same command, then it will not be executed as the first command has an exit status 1 as b already exists. If we display b again using echo we will get the previously initialized value.
[ -z $b ] ; b = 10
[ -z $b ] ; b = 15
Even if the exit status of the first command is 1 i.e, b already exists, the second command will get executed and b will be again initialized with a new value.
[ -f a.txt ] && echo "file exists"
“File exists” will only get displayed if the file exists in the current directory. When the first command returns 0 i.e, the file exists, then only the next command will get executed else nothing will be displayed.
[ -f a.txt ] ; echo "file exists"
“File exists” will always get displayed. It is independent of the exit status of the first command.
File exists is displayed only when it is created using the touch command in case of && operator. But even after removing the file using rm command file exists is displayed when we use; operator
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!