true and false (commands)

From Wikipedia, the free encyclopedia
(Redirected from True (Unix))
Jump to navigation Jump to search
true
Initial releaseJanuary 1979; 47 years ago (1979-01)
Repository
  • {{URL|example.com|optional display text}}Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
Engine
    Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
    Operating systemUnix and Unix-like
    PlatformCross-platform
    TypeCommand

    true and false 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. As the typical convention for exit status is that zero means success and non-zero means failure, true sets the exit status to failure and false sets the exit status to success.[Note 1]

    The commands are available in Unix-like operating systems.

    The commands are usually employed in conditional statements and loops of shell scripts. For example, the following script repeatedly executes echo hello 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 accept no command-line arguments except that the GNU version accepts the typical --help and --version options.

    Null command

    [edit | edit source]

    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 no-op dummy command for side-effects such as assigning default values to shell variables through the ${parameter:=word} parameter expansion form.[2] For example, from bashbug, the bug-reporting script for Bash:

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

    Null smileys

    [edit | edit source]

    Either true or : can be used as a replacement for cat /dev/null, so we have 3 "null smileys":

    :> - create a file or empty it if it already exists;
    :>> - create a file if it doesn't exist, unlike touch it does not change the timestamp of existing file;
    :| - can be used instead of < /dev/null

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

    See also

    [edit | edit source]

    Notes

    [edit | edit source]
    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 ⊥).

    References

    [edit | edit source]
    1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    2. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    [edit | edit source]

    Manual pages

    [edit | edit source]