true Command in Linux



The true command in Linux returns a successful exit status, 0, indicating success. It does not perform any action but can be used in scripts or commands where a success status is required, such as in loops or conditional structures.

Table of Contents

Here is a comprehensive guide to the options available with the true command in Linux −

Syntax of true Command

The syntax of the true command in Linux is as follows −

true [options]

In general, the true command does not require any argument, and anything passed as an argument is ignored. The [options] argument in the above syntax is optional, which is used to specify options mentioned in the next section.

Options of true Command

The true command does not require any arguments, and most implementations ignore any provided. However, in GNU coreutils, the following options are supported −

Option Description
--help To display help and exit
--version To display version information and exit

Using true Command in Linux

This section demonstrates how to use the true command in Linux with examples −

Using true Command with an Argument

The true command ignores any argument passed to it −

true any argument

Now, verify the exit code, run the following command −

echo $?
true Command in Linux1

Creating an Infinite Loop with the true Command

To set an infinite loop, the true command is used as an argument with while −

while true; do echo "Running..."; sleep 1; done
true Command in Linux2

In the above script, while true; starts a loop that runs as long as the condition is true. Because true command always returns a success status (0), the condition remains true indefinitely, creating an infinite loop.

The above script keeps on printing, running... every second until manually interrupted with CTRL+C.

Using true as a Placeholder in Conditional Statements

In the following example, the if statement checks the exit status of the command following it. Since true always succeeds, the condition is always true; as a result, the code inside the then block is always executed.

if true; then echo "true"; else echo "false"; fi
true Command in Linux3

Displaying Usage Help

To display the usage help of the true command, use the --help option −

true --help

The above command may not display help text, as many implementations ignore arguments.

Using the following command often works, depending on the system and coreutils version.

/usr/bin/true --help
true Command in Linux4

Conclusion

The true command in Linux is a simple utility that always returns a success status (exit code 0) without performing any operation. It is commonly used in scripting scenarios where a success condition is required, such as in loops, conditionals, or placeholders in scripts. The command can accept optional arguments, which are ignored, and provides basic options like --help and --version for displaying usage and version information.

In this tutorial, we explained the true command, its syntax, options, and usage in Linux with examples.

Advertisements