true and false (commands)
Initial release | January 1979 |
---|---|
Cross-platform | |
Type | Command |
In
logical values of command success, because true returns 0, and false returns 1.[Note 1]
Usage
The commands are usually employed in
loops of shell scripts
. For example, the following shell script repeats the echo hello loop until interrupted:
while true
do
echo hello
done
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's
FTP
. (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.
Null command
The true command is sometimes substituted with the very similar null command,[1] 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
:
while :
do
echo hello
done
The null command may take parameters, which are ignored. It is also used as a
bashbug, the bug-reporting script for Bash
:
: ${TMPDIR:=/tmp}
: ${EDITOR=$DEFEDITOR}
: ${USER=${LOGNAME-`whoami`}}
See also
- List of Unix commands
- Two-valued logic
- IEFBR14
Notes
- ^ These are distinct from the truth values of classical logic and most general purpose programming languages: true (1 or T) and false (0 or ⊥).
References
- ^ "Colon", The Open group base specifications, issue 7, IEEE std 1003.1-2008
- ^ Cooper, Mendel (April 2011), "Null command", Advanced Bash-scripting guide, 6.3, The Linux documentation project, retrieved 2011-08-04
External links
The Wikibook Guide to Unix has a page on the topic of: Commands
- The Single UNIX Specification, Version 4 from The Open Group
: return true value – Shell and Utilities Reference,
- The Single UNIX Specification, Version 4 from The Open Group : return false value – Shell and Utilities Reference,