Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The Bash if statement is a conditional control flow construct that branches execution based on the exit status of a specified command. Unlike many programming languages that evaluate boolean data types, Bash evaluates the integer exit code of a command. An exit status of 0 represents success (logical true), while any non-zero value (1-255) represents failure (logical false).

Core Syntax

The structural foundation of the if statement relies on the if, then, elif, else, and fi keywords.
if <command_or_test>; then
    <statements_executed_on_success>
elif <command_or_test>; then
    <statements_executed_on_secondary_success>
else
    <statements_executed_on_failure>
fi
The ; before then is a command separator, required if then is placed on the same line as the condition.

Evaluation Constructs

While any executable command can be used as the condition, Bash provides specific constructs for evaluating expressions, comparing values, and checking file attributes.

1. The test Command / Single Brackets [ ]

The [ token is a POSIX-compliant built-in command (equivalent to test). Because it is a command, strict whitespace is required between the brackets and the internal arguments. Variables must typically be quoted to prevent word splitting and globbing.
if [ "$VAR" -eq 10 ]; then
    # Executes if the test command returns exit status 0
fi

2. Extended Test Keyword / Double Brackets [[ ]]

The [[ token is a Bash-specific extended keyword. It suppresses word splitting and pathname expansion, making variable quoting largely optional. It also introduces advanced parsing capabilities, such as pattern matching and regular expression evaluation using the =~ operator.
if [[ $VAR == prefix* && $VAR =~ ^[a-z]+$ ]]; then
    # Executes if pattern and regex match
fi

3. Arithmetic Evaluation / Double Parentheses (( ))

The (( token invokes the arithmetic evaluation context. It allows for C-style mathematical comparisons. Unlike standard Bash exit codes, the expression inside (( )) evaluates mathematically: a non-zero mathematical result yields an exit status of 0 (true), and a mathematical zero yields an exit status of 1 (false).
if (( VAR > 5 && VAR <= 10 )); then
    # Executes if the mathematical expression is true
fi

4. Direct Command Execution

The if statement can directly evaluate the exit status of standard binaries or functions without using test brackets.
if grep -q "pattern" filename; then
    # Executes if grep finds the pattern (exit status 0)
fi

Operator Mechanics

The evaluation constructs utilize specific operators depending on the data type being compared:
  • Integer Operators (used in [ ] and [[ ]]): -eq (equal), -ne (not equal), -lt (less than), -le (less than or equal), -gt (greater than), -ge (greater than or equal).
  • String Operators: = or == (equal), != (not equal), -z (string length is zero), -n (string length is non-zero).
  • File Test Operators: -e (exists), -f (regular file), -d (directory), -x (executable), -s (size > 0).
  • Logical Operators:
    • Inside [ ]: -a (AND), -o (OR).
    • Inside [[ ]] and (( )): && (AND), || (OR).
    • Negation: ! (NOT) can be placed before the expression in any construct.
Master Bash with Deep Grasping Methodology!Learn More