Linux true command
What is the true command
true is one of the simplest commands in Linux/Unix systems; it has only one function:always return success (exit status code 0).
This command may seem simple, but it has important uses in shell scripting. It is equivalent to an operation that "does nothing but always succeeds".
Basic Syntax
Example
true
[
--help
]
[
--version
]
Option descriptions
| Option | Description |
|---|---|
| --help | Display help information and exit |
| --version | Display version information and exit |
How It Works
true The execution process of the command is very simple:
- When you enter in the terminal
trueand press Enter - the system executes this command
- the command completes immediately, doing nothing
- returns exit status code 0 (indicating success)
Practical Application Scenarios
1. As a condition for infinite loops
Example
while
true
;
do
echo
"This is an infinite loop"
sleep
1
done
In this example,while true creates an infinite loop, because true always returns success (0), so the loop condition is always true.
2. As a default success operation
Example
command
||
true
This notation means: if command fails, then execute true(always succeeds), which ensures the whole expression always returns a success status.
3. As a placeholder in scripts
Example
if
[
condition
]
;
then
:
Do nothing
else
true
can also indicate doing nothing, like this
fi
Here true as a substitute for a no-op, making the code structure clearer.
Comparison with the false command
There is also a corresponding false command in Linux, which always returns failure (exit status code 1). The following table shows their differences:
| Command | Function | Exit status code | Typical use |
|---|---|---|---|
| true | Do nothing, return success | 0 | Infinite loops, ignoring errors |
| false | Do nothing, return failure | 1 | Testing error handling, conditional judgment |
Implementation principle
On most Linux systems,true is a built-in command (shell builtin), but usually also has a standalone binary version:
Example
$
type
-a
true
true
is a shell
builtin
true
is
/
usr
/
bin
/
true
true
is
/
bin
/
true
Its implementation is extremely simple and can be expressed in C as:
Example
int
main
(
)
{
return
0
;
}
Practical Exercise
Try running directly in the terminal true, then check the exit status code:
Example
true
echo
$?
will output 0
Create an infinite loop script that outputs the current time every 5 seconds:
Example
#!/bin/bash
while
true
;
do
date
sleep
5
done
Testing true Behavior in conditional judgments:
Example
if
true
;
then
echo
"This will always execute"
fi
Frequently Asked Questions
Q: Does the true command really do nothing? A: Yes, it does not perform any actual operation; it simply returns a successful exit status.
Q: Why do we need such a seemingly useless command? A: In script programming, sometimes we need a placeholder or condition that always succeeds,true which exactly meets this need.
Q: What is the difference between true and the colon (:)? A: In most shells,: is also a command that does nothing, behaving the same as true . But : is a pure shell builtin, while true may have a standalone binary version.
Summary
true Although the command is simple, it is a practical tool in shell scripting. Its main features include:
- Always returns success (exit status code 0)
- Performs no actual operation
- Commonly used for infinite loops, error handling, and code placeholders
Linux Command Reference