Overview
true is a fundamental utility used in Unix and Unix-like operating systems. This command always returns a successful exit status (0), which can be utilized in shell scripting for purposes such as satisfying logical conditions or creating infinite loops.
Primary Uses
- Provides an always-true condition in shell scripts
- Creates infinite loops (e.g., while true)
- Serves as a successful starting point in command chains (e.g., true && command)
- For testing and debugging purposes
Key Options
The true command does not have functional options and does not process any arguments. As a standard utility, it only supports the `--help` and `--version` options.
Information
Generated command:
Try combining the commands.
Description:
`true` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Examples demonstrating how the true command can be utilized in shell scripts and the terminal.
Basic Usage
true
echo $?
The true command always returns an exit code of 0. You can check the exit code with echo $?.
Usage in a while loop
while true; do
echo "This message will be printed continuously."
sleep 1
done
Useful for creating infinite loops. You need to terminate it with Ctrl+C.
Usage in conditional statements
if true; then
echo "This message will always be printed."
fi
Used to create a condition that is always true.
Usage with logical operators
true && echo "Since true always succeeds, this message will be printed."
Used to ensure the execution of another command, or to indicate that a certain condition is always met.
Tips & Notes
The true command is simple yet can be very useful when writing scripts.
Comparison with the false command
true always returns success (exit code 0), while false always returns failure (exit code 1). These two commands are used complementarily for controlling the logical flow of shell scripts.
- true: Exit code 0 (Success)
- false: Exit code 1 (Failure)
Performance
The true command is very lightweight and executes quickly. It consumes minimal system resources, so its impact on performance is negligible even when called frequently within scripts.
Built-in vs. External Command
In most shells (like bash, zsh, etc.), true is provided as a shell built-in command, allowing for faster execution. However, it also exists as an external executable file, such as /bin/true.