> ## 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.

# Bash Conditional Expression

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:

```bash theme={"dark"}

# 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.

```bash theme={"dark"}
[ -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.

```bash theme={"dark"}
[ -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.

```bash theme={"dark"}
[ 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.

```bash theme={"dark"}

# 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.

```bash theme={"dark"}

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


# Inside double brackets, parentheses do not require escaping
[[ ( expression1 && expression2 ) || expression3 ]]
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
