> ## 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 Test Expression

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.

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

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

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

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