I've hacked on a lot of shell scripts, and sometimes the simplest things baffle me. Today I ran across a script that made extensive use of the :
(colon) bash builtin.
The documenation seems simple enough:
: (a colon) : [arguments]
Do nothing beyond expanding arguments and performing redirections. The return status is zero.
However I have previously only seen this used in demonstrations of shell expansion. The use case in the script I ran across made extensive use of this structure:
if [ -f ${file} ]; then
grep some_string ${file} >> otherfile || :
grep other_string ${file} >> otherfile || :
fi
There were actually hundreds of greps, but they are just more of the same. No input/output redirects are present other than the simple structure above. No return values are checked later in the script.
I am reading this as a useless construct that says "or do nothing". What purpose could ending these greps with "or do nothing" serve? In what case would this construct cause a different outcome than simply leaving off the || :
from all instances?
:
as an alternative totrue
. Perhapserrexit
is set and the author doesn't care about the exit status of some commands. – jw013 Feb 14 '12 at 18:42