True (Unix)
Encyclopedia
In Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s, true and false are commands whose only function is to always return the value 0 or 1.

Usage

The exit status
Exit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...

 of a command is often used to indicate success (zero status) or failure (non-zero) of the command. The true and false commands represent the logical value
Logical value
In logic and mathematics, a truth value, sometimes called a logical value, is a value indicating the relation of a proposition to truth.In classical logic, with its intended semantics, the truth values are true and false; that is, classical logic is a two-valued logic...

 of command success, because true returns 0, and false returns 1. The commands are usually employed in conditional statement
Conditional statement
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false...

s and loops of shell script
Shell script
A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language...

s. 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 login shell to 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 /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 some versions, 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 to the null command, written as a single colon (:). The null command is built into the shell
Shell builtin
In computing, a shell builtin is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute....

, and may therefore be more efficient if true is an external program. 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. For example, from bashbug, the bug-reporting script for Bash:
: ${TMPDIR
TMPDIR
TMPDIR is the canonical Unix environment variable that should be used to specify a temporary directory for scratch space. Most Unix programs will honor this setting and use its value to denote the scratch area for temporary files instead of the common default of /tmp or /var/tmp.Other forms...

:=/tmp}
: ${EDITOR=$DEFEDITOR}
: ${USER=${LOGNAME-`whoami`}}

Manual pages

  • true(1): Do nothing, successfully – GNU
    GNU
    GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

     Coreutils reference
  • false(1): Do nothing, unsuccessfully – GNU
    GNU
    GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

     Coreutils reference
  • true(1): Return true value - FreeBSD
    FreeBSD
    FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...

     manual page
  • false(1): Return false value - FreeBSD
    FreeBSD
    FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...

    manual page
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK