Skip to content
Related Articles

Related Articles

Difference Between && and ; chaining operators in Linux
  • Last Updated : 14 Oct, 2020

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.

Syntax:

command1 && command2

command2 will execute if command1 has executed successfully. This operator allows us to check the exit status of command1.

Semi-Colon Operator(;):



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.

Syntax:

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.

Example 1:

[ -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.

; and && operators

Example 2:

[ -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.

; and && operators

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :