true | |
Operating System: | Unix and Unix-like |
Platform: | Cross-platform |
Genre: | Command |
In Unix-like operating systems, true
and false
are commands whose only function is to always return with a predetermined exit status. Programmers and scripts often use the exit status of a command to assess success (exit status zero) or failure (non-zero) of the command. The true
and false
commands represent the logical values of command success, because true returns 0, and false returns 1.[1]
The commands are usually employed in conditional statements and loops of shell scripts. For example, the following shell script repeats the echo hello loop until interrupted:
The commands can be used to ignore the success or failure of a sequence of other commands, as in the example:
The programs take no "actual" parameters; in the GNU version, the standard parameter --help
displays a usage summary and --version
displays the program version.
The true command is sometimes substituted with the very similar null command, written as a single colon (:
). The null command is built into the shell, and may therefore be more efficient if true is an external program (true is usually a shell built in function). We can rewrite the upper example using :
instead of true
:
The null command may take parameters, which are ignored. It is also used as a no-op dummy command for side-effects such as assigning default values to shell variables through the ${parameter:=word}
parameter expansion form. For example, from bashbug, the bug-reporting script for Bash:
Do nothing, successfully – GNU Coreutils reference
Do nothing, unsuccessfully – GNU Coreutils reference
Return true value – FreeBSD manual page
Return false value – FreeBSD manual page