What is the utility of the : command in shell scripting, given that it explicitly does nothing?

I typically use true in loops; I think it's more clear:

while true; do
    ...
done

The one place I've found that : is really handy is in case statements, if you need to match something but don't want to actually do anything. For example:

case $answer in
    ([Yy]*) : ok ;;
    (*)     echo "stop."; exit 1 ;;
esac

Originally, it was used to determine that it was a Bourne shell program, as opposed to C compiled program. This was before shebang and multiple scripting languages (csh, perl). You can still run a script starting with just ::

$ echo : > /tmp/xyzzy
$ chmod +x /tmp/xyzzy
$ ./xyzzy

It will generally run the script against $SHELL (or /bin/sh).

Since then, the main use is to evaluate the arguments. I still use:

: ${EDITOR:=vim}

to set a default value in a script.


: is useful for writing loops that must be terminated from within.

while :
do
    ...stuff...
done

This will run forever unless break or exit is called, or the shell receives a terminating signal.