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.
true; echo $? # 0 false; echo $? # 1
Try it
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.
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
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 trueruns the commandtrueand tests its status. Writingx=true; if [ "$x" ]is a common bug: the non-empty stringfalseis also "true" to[. Useif $xor compare strings explicitly ([ "$x" = true ]). - Flag variables.
if $verboseis concise but runs whatever the variable contains, so only use it with values you set yourself. - Endless loops.
while true; do ...; doneis the portable idiom. The shorter:is equivalent and slightly faster in old shells, but less readable. - Placeholders. An empty function body or
thenbranch is a syntax error.true(or:) gives it a body. - Forcing success. Append
|| trueto a command whose failure is acceptable whenset -eorpipefailis active. - Forcing failure.
falseis handy for testing error handling:false || echo "handled". - Not the numbers 1 and 0.
truereturning0is the opposite of arithmetic truth, where(( 1 ))succeeds and(( 0 ))fails. - Login shell trick. Setting a user's shell to
/bin/falsemakessuandsshlog-ins exit at once.nologindoes the same with a friendly message.
Related
:,test,set,returnandexit, Exit status- Lessons: Exit codes and errors, Conditionals, Loops