Linux true command help and examples

true.gif

Updated: 06/22/2018 by Computer Hope

About true

“Do nothing, successfully.”

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 to always be true.

Syntax

true [anything ...]

With no arguments, true returns successfully.

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

Outputs nothing, with an exit status of 0.

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.)
...

false — Return an exit status of failure.
yes — Output “y”, or any other text, forever.
test — Test for file attributes, or compare values.

PinIt
submit to reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

Top
Web Design BangladeshWeb Design BangladeshMymensingh