true | |
---|---|
Initial release | January 1979; 46 years ago (1979-01) |
Operating system | Unix andUnix-like |
Platform | Cross-platform |
Type | Command |
InUnix-likeoperating systems,true
andfalse
are commands whose only function is to always return with a predeterminedexit 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. Thetrue
andfalse
commands represent thelogical values of command success, becausetrue returns 0, andfalse returns 1.[Note 1]
The commands are usually employed inconditional statements andloops ofshell scripts. For example, the following shell script repeats theecho hello loop until interrupted:
whiletruedoechohellodone
The commands can be used to ignore the success or failure of a sequence of other commands, as in the example:
make…&&false
Setting a user'slogin shell tofalse, in/etc/passwd, effectively denies them access to an interactive shell, but their account may still be valid for other services, such asFTP. (Although/sbin/nologin, if available, may be more fitting for this purpose, as it prints a notification before terminating the session.)
The programs take no "actual" parameters; in the GNU version, the standard parameter--help
displays a usage summary and--version
displays the program version.
Thetrue command is sometimes substituted with the very similar null command,[1] written as a single colon (:
). The null command isbuilt into the shell, and may therefore be more efficient iftrue is an external program (true is usually a shell built in function). We can rewrite the upper example using:
instead oftrue
:
while:doechohellodone
The null command may take parameters, which are ignored. It is also used as ano-op dummy command for side-effects such as assigning default values toshell variables through the${parameter:=word}
parameter expansion form.[2] For example, frombashbug, the bug-reporting script forBash:
:${TMPDIR:=/tmp}:${EDITOR=$DEFEDITOR}:${USER=${LOGNAME-`whoami`}}
true
: return true value – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Groupfalse
: return false value – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group