The BashDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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 theif statement relies on the if, then, elif, else, and fi keywords.
; 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.
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.
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).
4. Direct Command Execution
Theif statement can directly evaluate the exit status of standard binaries or functions without using test brackets.
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.
- Inside
Master Bash with Deep Grasping Methodology!Learn More





