Linux true Command
What is the true command
trueIt is one of the simplest commands in Linux/Unix systems, and it has only one function:Always return success (exit status code 0)。
This command seems simple, but it has important uses in shell script programming. It is equivalent to a "do nothing but always succeed" operation.
Basic Syntax
Examples
Options
| Option | Description |
|---|---|
| --help | Display help information and exit |
| --version | Display version information and exit |
How It Works
trueThe execution process of the command is very simple:
- When you enter
trueand press Enter - The system executes this command
- The command completes immediately without doing anything
- Returns exit status code 0 (indicating success)
Practical Use Cases
1. As a Condition for an Infinite Loop
Examples
echo "This is an infinite loop"
sleep 1
done
In this example,while truecreates an infinite loop becausetruealways returns success (0), so the loop condition is always true.
2. As a Default Successful Operation
Examples
This syntax means: ifcommandfails to execute, then executetrue(always succeeds), this ensures that the entire expression always returns a successful status.
3. As a Placeholder in Scripts
Examples
: # Do nothing
else
true # This can also be written to do nothing
fi
Heretrueserves as a substitute for a no-op, making the code structure clearer.
Comparison with the false Command
Linux also has a correspondingfalsecommand, which always returns failure (exit status code 1). The table below shows the differences between them:
| Command | Function | Exit status code | Typical use |
|---|---|---|---|
| true | Does nothing, returns success | 0 | Infinite loops, ignoring errors |
| false | Does nothing, returns failure | 1 | Testing error handling, conditional checks |
Implementation Details
In most Linux systems,trueis a built-in command (shell builtin), but there is usually also a standalone binary version:
Examples
true is a shell builtin
true is /usr/bin/true
true is /bin/true
Its implementation is extremely simple and can be represented in C as:
Examples
return 0;
}
Practical Exercises
Try running it directly in the terminaltrue, then check the exit status code:
Examples
echo $? # This will output 0
Create an infinite loop script that outputs the current time every 5 seconds:
Examples
while true; do
date
sleep 5
done
Testtruethe behavior in conditional statements:
Examples
echo "This will always execute"
fi
FAQ
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,trueand this command exactly meets that need.
Q: What is the difference between true and colon (:)?A: In most shells,:is also a command that does nothing, and its behavior is identicaltrueto true. However,:is a pure shell builtin, whiletruemay have a standalone binary version.
Summary
trueAlthough the command is simple, it is a practical tool in shell script programming. Its main features include:
- Always returns success (exit status code 0)
- Does not perform any actual operation
- Often used for infinite loops, error handling, and code placeholders
Linux Command Reference