Search This Blog

2015-05-08

Conditional Logic in Bash

What does the double pipe '||' mean?
It means that when the exit status of the previous command is NOT 0 then execute the next command.

The exit status is typically 0 upon success.

So this can be translated to: "If a command fails, execute the next command"

Example:

$ ls this_file_does_not_exist.txt || echo KO
ls: cannot access this_file_does_not_exist.txt: No such file or directory
KO


What does the double ampersand '&&' mean?
The opposite of '||' where if the exist status of the previous command IS 0 then execute the next command.

Typical exist status success is 0.

This is translated to: "If a command fails, do NOT execute the next command"

Example:

$ ls this_file_exist.txt && echo OK
this_file_exist.txt
OK

No comments:

Post a Comment