I was curious about "|| true
" and wrote the following pair of examples.
Example#1
#!/usr/bin/env bash
rm some_random_file_that_does_not_exist
echo "End of program"
When executed if produces the following result:
$ ./remove.sh
rm: cannot remove 'some_random_file_that_does_not_exist': No such file or directory
End of program
Example#2
#!/usr/bin/env bash
rm some_random_file_that_does_not_exist || true
echo "End of program"
Which produces the following result when executed:
$ ./remove.sh
rm: cannot remove 'some_random_file_that_does_not_exist': No such file or directory
End of program
The only difference I can tell is that in Example#1 the result code of the line that tries to remove the non-existent file is 1 while in Example#2 the result code is 0 (zero).
The way I understand this "|| true
" is to ensure that the execution of the command(s) at the left of the "||
" operator ends up with result code zero.
So my question is... Is there any other reason apart from this one that justifies the use of "|| true
" at the end of a command in Bash?
rm -f some_random_file_that_does_not_exist
. – Michael Mior Sep 08 '22 at 22:07