Linux Command Reference ยท Shell Builtins
The Bash : (colon) command
Reference for the Bash colon builtin: the null command that does nothing and succeeds, used for empty bodies, default assignments, and comment blocks.
The colon : is a builtin that does nothing and returns status 0. It is required by POSIX, so it works in every shell. What makes it more than a curiosity is that its arguments are still expanded before being thrown away. That side effect is what turns it into a tool: : ${VAR:=default} assigns a default without running any real command.
It is also the traditional placeholder where the grammar demands a command but you have nothing to run yet, such as an empty then branch or an endless loop condition.
: ${LOG_DIR:=/var/log/app} while :; do work; done
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
: [ARGUMENTS...]
| Name | Description |
|---|---|
ARGUMENTS |
Expanded as usual (variables, command substitution, arithmetic, ${var:=x}), then discarded. |
Returns: always 0, unless an expansion fails (for example ${var:?} on an unset variable), in which case the shell reports the error and a non-interactive shell exits.
Edge cases: redirections on : still happen, so : > file creates or empties a file.
Usage notes
- Defaults.
: "${VAR:=value}"is the classic idiom for "setVARto this if it has no value". Quote the expansion to avoid word splitting of the throwaway result. - Required values.
: "${VAR:?message}"stops the script withmessagewhenVARis unset or empty. - Space required.
:is a command word, so:foois a command named:foo. Write: foo. - Empty bodies. Functions,
thenbranches andcasearms may not be empty;:fills the gap.trueworks the same and reads more clearly to beginners. - Endless loops.
while :; do ...; doneis common in older scripts. - Block comments.
: <<'EOF' ... EOFswallows many lines. Quote the delimiter or the text is still expanded, including$(...). Most editors make real#comments easier to maintain. - Truncating files.
: > fileis a portable way to empty a file without a subshell orcat. - Not the same as
#. A comment is removed by the parser;:runs and expands its arguments, so side effects and errors inside it are real. - Debugging. With
set -x,: labellines show up in the trace and can act as visible markers.