{{short description |Shell commands that exit immediately with a 0/1 status}} {{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 = }} '''<code>true</code>''' and '''<code>false</code>''' are shell commands that exit immediately with exit status 0 or 1, respectively. As a script sets its process exit status to the value of the last command it runs, these commands can be used to set the exit status of a script run. All Unix shells interpret an exit status of zero as success and non-zero (usually) as failure, so {{code |true}} sets success and {{code |false}} sets failure.<ref group=Note>These are distinct from the truth values of classical logic and most general purpose programming languages: true (1 or T) and false (0 or ⊥). Note that a built-in {{code |true}} sets the exit status, while the program {{code |true}} returns it, with the same effect.</ref>

The commands are available in Unix-like operating systems.

==Use== The commands are usually employed in conditional statements and loops of shell scripts. For example, the following script repeatedly executes {{code|echo hello}} until interrupted:

<syntaxhighlight lang="bash"> while true do echo hello done </syntaxhighlight>

The commands can be used to ignore the success or failure of a sequence of other commands, as in the example: <syntaxhighlight lang="bash">make … && false</syntaxhighlight> Setting a user's login shell to {{mono|'''false'''}}, in {{mono|/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 accept no command-line arguments except that the GNU version accepts the typical <code>--help</code> and <code>--version</code> options.

==Null command== The ''true'' command is sometimes substituted with the very similar null command,<ref>{{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 |access-date=2011-08-04 |archive-date=2014-04-27 |archive-url=https://web.archive.org/web/20140427082439/http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_16 |url-status=live }}</ref> written as a single colon (<code>:</code>). 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 <code>:</code> instead of <code>true</code>:

<syntaxhighlight lang="bash"> while : do echo hello done </syntaxhighlight>

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 <code>${parameter:=word}</code> parameter expansion form.<ref>{{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 |archive-date=2019-06-03 |archive-url=https://web.archive.org/web/20190603071617/http://tldp.org/LDP/abs/html/special-chars.html#COLON0REF |url-status=live }}</ref> For example, from ''bashbug'', the bug-reporting script for Bash:

<syntaxhighlight lang="bash"> : ${TMPDIR:=/tmp} : ${EDITOR=$DEFEDITOR} : ${USER=${LOGNAME-`whoami`}} </syntaxhighlight>

==Null smileys== Either <code>true</code> or <code>:</code> can be used as a replacement for <code>cat /dev/null</code>, so there are 3 "null smileys": :<code>:></code> - create a file or empty it if it already exists; :<code>:>></code> - create a file if it doesn't exist, unlike <code>touch</code> it does not change the timestamp of existing file; :<code>:|</code> - can be used instead of <code>< /dev/null</code>

Such usage is similar to the IEFBR14's standard usage.

==See also== * {{Annotated link |IEFBR14}} * {{Annotated link |List of POSIX commands}} * {{Annotated link |Two-valued logic}}

==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 Category:Unix SUS2008 utilities Category:IBM i Qshell commands