What purpose does the colon builtin ':' serve?
56
votes
7
answers
15861
views
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?
Asked by Caleb
(71790 rep)
Feb 14, 2012, 06:37 PM
Last activity: Feb 25, 2024, 11:24 AM
Last activity: Feb 25, 2024, 11:24 AM