&&

From LQWiki
Jump to navigation Jump to search

&& is a famous operator in the C language and inherited by most scripting languages including shells, perl, python and such. It is for conditional execution: in a shell command, for instance, if the left command succeeded, the right one will be executed, else not.

Example:

ls /etc/SuSE-release && cat /etc/SuSE-release

This will only show the content of /etc/SuSE-release if this file could be listed.

Why?

&& means a logical AND. Now, if we have

command1 && command2

and command1 is successful, it delivers the return code 0 (true). That means, command2 must be evaluated to see if it makes the expression false. Now if command1 is unsuccessful, it delivers a return code unequal 0 (so, false). That means, command2 does not even need to be evaluated, the expression will be false in any case. So, command2 is not even executed.

See also