my-server
← Wiki Redirected from True (Unix)

True and false (commands)

<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 sets success and sets failure.

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 until interrupted:

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 , in , effectively denies them access to an interactive shell, but their account may still be valid for other services, such as FTP. (Although , 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, 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>:

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. For example, from bashbug, the bug-reporting script for Bash:

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

Notes

References

External links

Manual pages