bash suffix `||:`
It only returns success if the command before it did not return success.
With set -e
(aka set -o errexit
), a command line returning an error would abort the script. If you add this ||:
to the end of a command line, there will be no error (because, if there were, the next command would return true).
It basically says: if there was an error in the previous command, run true
so that the command line ends without an error and the script can go on.
This script will abort without displaying the date because ls
returns an error:
set -e
ls -l /tmp/nonexistentfile
date
But this script will not abort and it will display the date, since the last command is :
(true
):
set -e
ls -l /tmp/nonexistentfile || :
date
There are two separate pieces of syntax here:
|| This is the OR symbol.
: This is a dummy command which will return a success (return code 0)
In combination with a command this has the effect of first running the command and having the line return 'success' irrespective of the return code of the first command. Since either the command will return True, OR :
(do nothing successfully) will be returned.
Separately set -e
has the effect of exiting a script if any statement in that script returns False. Therefore ||:
is acting as a 'guard', ensuring the script continues even if the command on that line returns False