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 test expression evaluates a conditional statement and returns an exit status indicating success (0 for true) or failure (1 for false). It is the foundational mechanism for boolean logic in Bash, relying on shell builtins or keywords to parse arguments and operators.

Syntax Constructs

Bash provides three primary constructs for evaluating test expressions: 1. The test Builtin The traditional POSIX-compliant command.
test expression
2. The Single Bracket [ ] Builtin A syntactic alias for the test command. It requires a closing ] as its final argument. Because it is parsed as a standard command, unquoted variables within the brackets are subject to word splitting and pathname expansion (globbing).
[ expression ]
3. The Double Bracket [[ ]] Keyword A Bash-specific extended test construct. Because it is a shell keyword rather than a command, it suppresses word splitting and pathname expansion for its internal arguments. It also supports advanced pattern matching and regular expressions.
[[ expression ]]

Operator Categories

Test expressions utilize specific unary and binary operators to evaluate data types, string literals, and file metadata.

File Test Operators

Evaluate file system attributes and metadata.
  • -e file : True if the file exists.
  • -f file : True if the file is a regular file.
  • -d file : True if the file is a directory.
  • -s file : True if the file exists and has a size greater than zero.
  • -r file : True if the file has read permission.
  • -w file : True if the file has write permission.
  • -x file : True if the file has execute permission.

String Operators

Evaluate string length and lexicographical equivalence.
  • -z string : True if the string length is exactly zero.
  • -n string : True if the string length is non-zero.
  • str1 = str2 : True if strings are identical (POSIX standard). Within [[ ]], the right operand is treated as a glob pattern rather than a literal string unless quoted.
  • str1 == str2: Synonym for =. True if strings are identical. Within [[ ]], the right operand is treated as a glob pattern rather than a literal string unless quoted.
  • str1 != str2: True if strings are not identical.
  • str1 < str2 : True if str1 sorts before str2 lexicographically (requires escaping \< in [ ]).
  • str1 > str2 : True if str1 sorts after str2 lexicographically (requires escaping \> in [ ]).
  • str1 =~ regex: True if str1 matches the extended regular expression regex (Valid only within [[ ]]).

Integer Operators

Evaluate algebraic relationships between integers. Within [ ] and test, these operators strictly require numeric literals or expanded variables. Within [[ ]], these operators force an arithmetic evaluation context, meaning they can evaluate variables without the $ prefix and unquoted arithmetic expressions (e.g., [[ x+2 -eq 5 ]]).
  • 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

Combine or invert multiple expressions. The syntax depends on whether the operator is used internally within a construct or externally as a shell control operator.
  • Negation (NOT):
    • ! expression : Inverts the exit status. Valid internally within all constructs.
  • Logical AND:
    • expr1 -a expr2 : Internal AND operator. Valid only within [ ] or test (POSIX obsolescent).
    • expr1 && expr2 : Internal AND operator within [[ ]]. Outside of test constructs, && is a standard shell control operator used to chain multiple [ ] or test commands (e.g., [ expr1 ] && [ expr2 ]).
  • Logical OR:
    • expr1 -o expr2 : Internal OR operator. Valid only within [ ] or test (POSIX obsolescent).
    • expr1 || expr2 : Internal OR operator within [[ ]]. Outside of test constructs, || is a standard shell control operator used to chain multiple [ ] or test commands (e.g., [ expr1 ] || [ expr2 ]).
Master Bash with Deep Grasping Methodology!Learn More