True and false (commands)
{{short description|Standard Unix utility}}
{{lowercase title}}
{{Infobox software
| name = true
| logo =
| screenshot =
| screenshot size =
| caption =
| author =
| developer =
| released = {{Start date and age|1979|1}}
| latest release version =
| latest release date =
| operating system = Unix and Unix-like
| platform = Cross-platform
| genre = Command
| license =
| website =
}}
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.These are distinct from the truth values of classical logic and most general purpose programming languages: true (1 or T) and false (0 or ⊥).
Usage
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:
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:
Setting a user's login shell to {{mono|false}}, in /etc/passwd, effectively denies them access to an interactive shell, but their account may still be valid for other services, such as FTP. (Although {{mono|/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,{{citation
|url=http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_16
|title=Colon |work=The Open group base specifications, issue 7
|id=IEEE std 1003.1-2008}} 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 no-op dummy command for side-effects such as assigning default values to shell variables through the ${parameter:=word}
parameter expansion form.{{citation
|chapter-url=http://tldp.org/LDP/abs/html/special-chars.html#COLON0REF |access-date=2011-08-04
|chapter=Null command |title=Advanced Bash-scripting guide, 6.3
|first=Mendel |last=Cooper |date=April 2011
|publisher=The Linux documentation project}} For example, from bashbug, the bug-reporting script for Bash:
: ${TMPDIR:=/tmp}
: ${EDITOR=$DEFEDITOR}
: ${USER=${LOGNAME-`whoami`}}
See also
Notes
{{Reflist|group=Note}}
References
{{Reflist}}
External links
{{Wikibooks|Guide to Unix|Commands}}
- {{man|cu|true|SUS|return true value}}
- {{man|cu|false|SUS|return false value}}
=Manual pages=
- [https://www.gnu.org/software/coreutils/manual/html_node/true-invocation.html true(1)]: Do nothing, successfully – GNU Coreutils reference
- [https://www.gnu.org/software/coreutils/manual/html_node/false-invocation.html false(1)]: Do nothing, unsuccessfully – GNU Coreutils reference
- [http://man.freebsd.org/true true(1)]: Return true value – FreeBSD manual page
- [http://man.freebsd.org/false false(1)]: Return false value – FreeBSD manual page
{{Unix commands}}
{{Core Utilities commands}}
Category:Standard Unix programs