LinuxCommandLibrary

colon

Do nothing; it's a shell built-in

TLDR

Return a successful exit code

$ :
copy

Make a command always exit with 0
$ [command] || :
copy

SYNOPSIS

: [arguments...]

The colon command accepts any number of arguments, but it ignores them completely. Its behavior remains unchanged regardless of the arguments provided.

PARAMETERS

arguments...
    Any arguments provided to the colon command are parsed but subsequently ignored. The command's behavior and exit status remain unaffected.

DESCRIPTION

The colon command, represented by the single character :, is a fundamental shell builtin rather than an external executable program. It performs no operation, effectively acting as a "no-op" or null command. Its primary characteristic is that it always exits with a true (zero) status code. This makes it incredibly versatile in shell scripting.

Common uses include creating infinite loops (e.g., while :; do ... done), serving as a placeholder when a command is syntactically required but no action is desired, and as a target for shell parameter expansion without assigning a value (e.g., :${VAR:?error_msg}). It's also frequently used as a separator character in environment variables like PATH or IFS, and can act as a simple "comment" if placed at the beginning of a line, though # is the preferred comment character. Its simplicity and consistent true return status make it an indispensable tool for shell scripters.

CAVEATS

  • The colon command (:) is a shell builtin, meaning it's an integral part of the shell itself (e.g., Bash, Zsh) and not a separate executable file found in the system's PATH.
  • Although it accepts arguments, these arguments are merely parsed and then discarded, having no effect on the command's operation or exit status.
  • It should not be confused with the true command, which also returns a zero exit status but is typically an external executable (though some shells implement true as a builtin as well).
  • Using : at the beginning of a line can effectively "comment out" the rest of the line, similar to #, but # is the standard and more readable convention for comments.

USAGE IN LOOPS

The colon command is commonly used to create infinite loops, often in while or until constructs, because it always returns a true exit status. For example: while :; do echo 'Looping indefinitely...'; sleep 1; done

PARAMETER EXPANSION

It can be used in shell parameter expansion without affecting a variable's value or as a default target. For instance, :${VAR:?ERROR: VAR not set} will check if VAR is set and exit with an error if it isn't, without requiring an actual command to follow the colon.

PLACEHOLDER / NO-OP

When a command is syntactically required but no action is desired, : serves as an excellent placeholder. For example, in a case statement where a specific pattern should do nothing: case $VAR in pattern) :;; esac

AS A SEPARATOR

The colon character is widely used as a separator in various environment variables, most notably PATH (for directory lists) and IFS (Internal Field Separator). While this is a usage of the character, it's distinct from its command form, but highlights its special significance.

HISTORY

The colon command (:) has been a fundamental part of Unix shells since the very early days, specifically present in the original Bourne Shell (sh) developed by Stephen Bourne. Its inclusion as a simple no-operation command with a guaranteed true exit status made it essential for various scripting paradigms from the outset. Its functionality has remained consistent across virtually all modern Unix-like shells, including bash, zsh, ksh, and dash, underscoring its foundational role in shell scripting.

SEE ALSO

true(1), false(1), bash(1), sh(1), test(1)

Copied to clipboard