Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Linux true and false commands

Reference for true and false: commands that do nothing and return exit status 0 or 1, used for infinite loops, placeholders, default values and testing.

true does nothing and succeeds: its exit status is 0. false does nothing and fails: its exit status is 1. They ignore their arguments and print nothing. In the shell, success and failure are the only truth values there are, so these two commands are the closest thing to boolean literals.

Bash has them as builtins, and coreutils also ships true and false (in /bin or /usr/bin, depending on the distribution) as separate programs. The programs matter in places where no shell builtin exists, such as the login shell field of /etc/passwd, where /usr/sbin/nologin or /bin/false blocks interactive logins.

Terminal
true;  echo $?    # 0
false; echo $?    # 1

Try it

Bash Exit statuses and conditions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Loops and placeholders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
true
false
Name Description
(arguments) Ignored. Both commands accept and discard any arguments.

Returns: true returns 0, false returns 1.

Edge cases: the GNU programs also understand --help and --version when they are the only argument, but the Bash builtins treat those as ordinary ignored arguments.

Usage notes

  • They are commands, not values. if true runs the command true and tests its status. Writing x=true; if [ "$x" ] is a common bug: the non-empty string false is also "true" to [. Use if $x or compare strings explicitly ([ "$x" = true ]).
  • Flag variables. if $verbose is concise but runs whatever the variable contains, so only use it with values you set yourself.
  • Endless loops. while true; do ...; done is the portable idiom. The shorter : is equivalent and slightly faster in old shells, but less readable.
  • Placeholders. An empty function body or then branch is a syntax error. true (or :) gives it a body.
  • Forcing success. Append || true to a command whose failure is acceptable when set -e or pipefail is active.
  • Forcing failure. false is handy for testing error handling: false || echo "handled".
  • Not the numbers 1 and 0. true returning 0 is the opposite of arithmetic truth, where (( 1 )) succeeds and (( 0 )) fails.
  • Login shell trick. Setting a user's shell to /bin/false makes su and ssh log-ins exit at once. nologin does the same with a friendly message.