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.

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:

# 1. The test builtin command
test expression


# 2. Single brackets (POSIX standard, alias for test)
[ expression ]


# 3. Double brackets (Bash extended shell keyword)
[[ expression ]]

[ ] 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.
[ -e FILE ]  # True if file exists (any type)
[ -f FILE ]  # True if file exists and is a regular file
[ -d FILE ]  # True if file exists and is a directory
[ -s FILE ]  # True if file exists and has a size greater than zero
[ -r FILE ]  # True if file exists and is readable
[ -w FILE ]  # True if file exists and is writable
[ -x FILE ]  # True if file exists and is executable
[ -L FILE ]  # True if file exists and is a symbolic link

String Operators

Operators used to evaluate string length and perform lexicographical comparisons.
[ -z STRING ]      # True if the length of the string is zero
[ -n STRING ]      # True if the length of the string is non-zero
[ STRING1 = STRING2 ]   # True if strings are identical (== is also valid)
[ STRING1 != STRING2 ]  # True if strings are not identical
[[ STRING1 < STRING2 ]] # True if STRING1 sorts before STRING2 lexicographically
[[ STRING =~ REGEX ]]   # True if STRING matches the extended regular expression REGEX
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.
[ INT1 -eq INT2 ]  # Equal to
[ INT1 -ne INT2 ]  # Not equal to
[ INT1 -lt INT2 ]  # Less than
[ INT1 -le INT2 ]  # Less than or equal to
[ INT1 -gt INT2 ]  # Greater than
[ INT1 -ge INT2 ]  # Greater than or equal to

Logical Operators

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

# Negation (Unary)
[ ! expression ]
[[ ! expression ]]


# Logical AND (Binary)
[ expression1 -a expression2 ]
[[ expression1 && expression2 ]]


# Logical OR (Binary)
[ expression1 -o expression2 ]
[[ expression1 || expression2 ]]

Grouping and Precedence

In complex expressions, precedence can be explicitly defined using parentheses.

# Inside single brackets, parentheses must be escaped
[ \( expression1 -a expression2 \) -o expression3 ]


# Inside double brackets, parentheses do not require escaping
[[ ( expression1 && expression2 ) || expression3 ]]
Master Bash with Deep Grasping Methodology!Learn More