Bash — and/or/no-care chains, how to make your life easier

What are and/or/no-care chains?

What are and/or/no-care chains?

and/or/no-care chains are bash syntactic sugar that allows users to execute a chain of commands and decide which command to be executed on this chain based on the exit code of the previous command.

Examples:

Linux has a command named true its usage is very simple to understand, it exists with exit code 0 and this can be verified with the $? variable which holds the exit code of the last executed command.kpatronas@narcissus:~$ true
kpatronas@narcissus:~$ echo $?
0

As you can guess there is also the false command that exits always with exit code 1kpatronas@narcissus:~$ false
kpatronas@narcissus:~$ echo $?
1

You might wondering why we talk about those commands, using those commands we can simulate possible scenarios of exit statuses of commands, any command, program or script that uses good coding standards will exit with zero if the execution was successful and with a non-zero code if the execution was not successful

and example:

An and chain is implemented using the && operators, the following example means execute echo "This is true" only if the exit code of true is equal to zero.kpatronas@narcissus:~$ true && echo "This is true"
This is true

Checking the exit status of echo "This is true" we can see that returns zerokpatronas@narcissus:~$ echo $?
0

Lets chain another command using the && operator again, we can see that both echo commands executed because all commands in the chain return an exit code of zero.kpatronas@narcissus:~$ true && echo "This is true" && echo "This is also true"
This is true
This is also true

or example:

Using the || operator we can execute a command only if the previous one had returned a non-zero exit code, in this case the second echo statement did not executed because the first echo returned an exit status of zero.kpatronas@narcissus:~$ true && echo "This is true" || echo "This is also true"
This is true

Replacing the first echo command with the false command we can see that the echo statement chained with the || operator is executedkpatronas@narcissus:~$ true && false || echo "This is also true"
This is also true

no-care example:

using ; allows us to chain commands to be executed without caring for the exit code of the previous command, in those examples the last echo commands are executed without caring about the exit code of false or echo "This is also true" commands.kpatronas@narcissus:~$ true && false || echo "This is also true";echo "bye-bye"
This is also true
bye-bye
kpatronas@narcissus:~$ true && false || false;echo "bye-bye"
bye-bye

A practical example:

Lets see a real life example scenario, in this case we did the following

  1. We created a directory named test and we get into
  2. We tried to list file myfile.txt that does not exist
  3. The second echo command executed only because the exit code of ls was not zero since the file myfile.txt does not exist.
  4. The last echo command executed because the ; operator does not care of the exit code of the last command$mkdir ~/test
    $ cd ./test
    ~/test$ ls myfile.txt && echo "File exists" || echo "File does not exist";echo "bye :)"
    ls: cannot access 'myfile.txt': No such file or directory
    File does not exist
    bye :)

Now lets create the file myfile.txt and re-run the commandtouch myfile.txt
~/test$ ls myfile.txt && echo "File exists" || echo "File does not exist";echo "bye :)"
myfile.txt
File exists
bye :)

What happened now

  1. The second echo command did not executed because the exit status of ls was zero since file myfile.txt exists now
  2. The third echo command executed because the ; operator does not care of the execution status of the last command

I hope you found this article useful :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…