Linux true Command

What is Linux true Command?

In this tutorial we learn how to use true command in Linux. true command in Linux will do nothing, successfully. This command is the opposite of false command.

true Syntax

true [ignored command line arguments]
true OPTION

Using true command in Linux

To use true we can invoke the command without any options or parameters.

true

true command will gives us no output or anything at all. But we can check the exit status of the true command using the command below right after invoking true command.

echo $?
0

The exit status 0 above shows that the command executed successfully.

Using true Command To Force Exit Status 0 in Linux Script

You might be wondering what is the use case of Linux true command? You will usually this command being found in a shell script to make sure the command that we have provides exit status 0.

The next question will be, why we want to make sure we get exit status 0? In a shell script if we don’t get exit status 0 the script will be terminated and exit. For non critical command we can make sure that the command will still continue to run even though the command is not successfully run.

The format of the command will be similar to

COMMAND || true

The double pipe || above means OR. If the first command (command on the left) successfully run, the second command will not be invoked, but if the first command failed. The second command will be invoked.

Since the second command is true, then the exit status will always be 0. For this tutorial, let’s create a new directory named private directory.

mkdir privatedir

If we tried to remove the directory it will be failed since it is a directory.

$ rm -f privatedir/
rm: cannot remove 'privatedir/': Is a directory

If we check the exit status of the command above. The exit status will be 1

echo $?
1

Now if we run the command to remove privatedir directory above but with true command. The exit status will be 0.

rm -f privatedir/ || true
rm: cannot remove 'privatedir/': Is a directory

It will still show error message that the removal failed but if we check the exit status, the exit status will be 0

echo $?
0

The sample above is the most common usage of true command in Linux.

true Command Manual / Help

We can use man and info command to see the manual page of true command.

true command also have code –help option to show list of options.

man page of the true command can be opened using the command below. To exit man or info page you can press q.

man true

Info page for the true command can be opened using command below.

info true

To open help page from true command we can run command below.

true --help

Linux true Command Source Code

You can find true command source code from the folowing repositories:

You can read tutorials of related Linux commands below:

Summary

In this tutorial we learn how to use true in Linux with practical examples. Visit our Linux Commands guide to learn more about using command line interface in Linux.

1 thought on “Linux true Command”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.