What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself?
It’s slower than inserting a comment into your scripts by about 40% per call, which probably varies greatly depending on the size of the comment. The only possible reasons I can see for it are these:
# poor man's delay function
for ((x=0;x<100000;++x)) ; do : ; done
# inserting comments into string of commands
command ; command ; : we need a comment in here for some reason ; command
# an alias for `true'
while : ; do command ; done
I guess what I’m really looking for is what historical application it might have had.
Historically, Bourne shells didn’t have
trueandfalseas built-in commands.truewas instead simply aliased to:, andfalseto something likelet 0.:is slightly better thantruefor portability to ancient Bourne-derived shells. As a simple example, consider having neither the!pipeline operator nor the||list operator (as was the case for some ancient Bourne shells). This leaves theelseclause of theifstatement as the only means for branching based on exit status:Since
ifrequires a non-emptythenclause and comments don’t count as non-empty,:serves as a no-op.Nowadays (that is: in a modern context) you can usually use either
:ortrue. Both are specified by POSIX, and some findtrueeasier to read. However there is one interesting difference::is a so-called POSIX special built-in, whereastrueis a regular built-in.Special built-ins are required to be built into the shell; Regular built-ins are only “typically” built in, but it isn’t strictly guaranteed. There usually shouldn’t be a regular program named
:with the function oftruein PATH of most systems.Probably the most crucial difference is that with special built-ins, any variable set by the built-in – even in the environment during simple command evaluation – persists after the command completes, as demonstrated here using ksh93:
Note that Zsh ignores this requirement, as does GNU Bash except when operating in POSIX compatibility mode, but all other major “POSIX sh derived” shells observe this including dash, ksh93, and mksh.
Another difference is that regular built-ins must be compatible with
exec– demonstrated here using Bash:POSIX also explicitly notes that
:may be faster thantrue, though this is of course an implementation-specific detail.