“Do nothing, successfully.”
On Unix-like operating systems, the true command’s sole purpose is to return a successful exit status. It is useful when you want part of a command or conditional expression always to be true.
This page covers the GNU/Linux version of true.
Syntax
true [anything …]
When provided no arguments, true returns successfully.
Syntax
Exit status
Examples
Linux commands help
Exit status
When provided any number of arguments, true returns successfully.
Exit status
The true command always returns 0, representing “true” or “success”.
Examples
true
Outputs nothing, with an exit status of 0.
true false maybe-true 5 file.txt -f -p
You can verify the exit status by checking the value of the special shell variable ?, which contains the exit status of the previous command. For instance, in the next command, we use a semicolon (;) to execute two commands in one line. The second command is echo, which prints the value of ?, which like all shell variables may be referenced by prefixing the variable name with $.
true; echo “The previous command’s exit status is $?.”
The previous command’s exit status is 0.
Two examples of using true in an “if” statement:
if true; then echo “True”; else echo “False”; fi
True
The next command uses the ! reserved bash character to negate the return value of the pipeline that follows. In other words, if ! true is equivalent to if false. (For more information, see: Pipelines in bash.)
if ! true; then echo “True”; else echo “False”; fi
False
An example of using true in a “while” statement:
while true; do echo “This will run forever. Press CTRL+C to terminate.”; echo “(So will this.)”; done
This will run forever. Press CTRL+C to terminate. (So will this.) This will run forever. Press CTRL+C to terminate. (So will this.) This will run forever. Press CTRL+C to terminate. (So will this.) …
Related commands
false — Return an exit status of failure.yes — Output “y”, or any other text, forever.test — Test for file attributes, or compare values.