What purpose does the colon builtin serve?
The :
builtin is also useful with the Bash "assign default values" shell expansion, where the expansion is often used solely for the side effect and the value expanded is thrown away:
# assign FOO=bar iff FOO is unset
: ${FOO:=bar}
It appears the :
s in your script are being used in lieu of true
. If grep
doesn't find a match in the file, it will return a nonzero exit code; as jw013 mentions in a comment, if errexit
is set, probably by -e
on the shebang line, the script would exit if any of the grep
s fail to find a match. Clearly, that's not what the author wanted, so (s)he added || :
to make the exit status of that particular compound command always zero, like the more common (in my experience) || true
/|| /bin/true
.
I can think of two places I've used :
in the past.
while :
do
shell commands
some exit condition
done
That is a forever-loop.
function doSomethingStub {
:
}
Put in a stub function, just to get top level flow of control correct.
One use I've seen back in the Old Days: Instead of a #!/bin/sh
(or whatever) line, you'd see a :
line. Some of the older Real Unix kernels or Real Unix shells would use that to mean "I'm a shell script, have sh run me". As I recall this was just when csh was making inroads as a common interactive shell.