true - Linux


Overview

The true command in Linux is a simple utility that exits with a status of zero (0), which is conventionally used in shell scripts to represent “success” or a “true” logical condition. Its primary role is in conditional scripting, loops, and placeholders for functions. It is particularly useful for creating infinite loops, or as a no-operation (NOP) command in scripts where a syntactically necessary command that does not perform any action is needed.

Syntax

The true command has a very straightforward syntax as it does not take any arguments:

true

Options/Flags

true does not have any options or flags. Its behavior is unchangeable, making it uniquely simple and reliable for its intended use cases.

Examples

  1. Basic Usage:
    Simply executing the command:

    true
    
  2. Infinite Loop:
    Using true in an infinite loop:

    while true; do
        echo "This will keep printing indefinitely."
        sleep 1  # Pauses for 1 second to prevent system overload.
    done
    
  3. Placeholder in a script:
    Demonstrating a place where a command might go later:

    if some_condition; then
        true  # Placeholder for future command
    else
        echo "Condition not met."
    fi
    

Common Issues

Since true does not perform any operations or have any options, it’s rare to encounter issues directly related to its use. However, users could face problems if:

  • Misunderstanding its Purpose: Mistaking true for a command that checks or validates conditions (it does not; it merely exits with a status of 0).

Solution: Understand that true is intended to always return 0 and should be used where this behavior is beneficial, such as in conditional scripts where only the exit status is important.

Integration

true can be integrated with other commands in scripts to facilitate desired control-flows. For example, when paired with the watch command, it can be used to execute non-intrusive repetitions:

watch true

This command repeats true every 2 seconds by default but essentially does nothing, allowing you to keep a command window actively open with minimal system impact.

  • false: Returns a non-zero exit status, typically used to represent a “failure” or “false” condition in scripts.
  • : (colon): Another NOP (no operation) command similar to true, often used in shell scripts.

Additional resources for more advanced script structuring and command usage can be found in Bash scripting tutorials or the GNU Core Utilities manual page for true: