Skip to main content
A Bash conditional expression evaluates a specific condition and yields an exit status of 0 (true) or 1 (false). These expressions are evaluated using test constructs and are the primary mechanism for resolving boolean logic in shell control flow structures.

Evaluation Constructs

Bash provides three primary constructs for evaluating conditional expressions:

[ ] vs [[ ]]

  • [ ] (POSIX test): Evaluated as a standard command. Variables inside single brackets are subject to word splitting and pathname expansion (globbing). Variables must typically be quoted (e.g., [ "$VAR" = "string" ]) to prevent syntax errors if the variable is empty or contains spaces.
  • [[ ]] (Bash Keyword): Evaluated as a shell keyword. It suppresses word splitting and pathname expansion for its internal arguments. It introduces advanced features like unquoted variable safety, pattern matching (globbing) on the right-hand side of ==, and regular expression matching via =~.

Operator Categories

Conditional expressions rely on unary and binary operators to perform evaluations.

File Test Operators

Unary operators used to evaluate file attributes and existence.

String Operators

Operators used to evaluate string length and perform lexicographical comparisons.
Note: The < and > operators must be escaped (\<, \>) when used inside single brackets [ ] to prevent them from being interpreted as redirection operators.

Integer Operators

Binary operators strictly used for algebraic comparison of integers.

Logical Operators

Operators used to combine or negate expressions. The syntax differs strictly between the POSIX [ ] and Bash [[ ]] constructs.

Grouping and Precedence

In complex expressions, precedence can be explicitly defined using parentheses.
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More