> ## Documentation Index
> Fetch the complete documentation index at: https://siriusbar.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Operators

> String, integer, file, and logical operators in Bash.

This is tricky because Bash variables are string-like by default, but conditional expressions can compare values as either text or integers. So we have separate operators for strings and integers.

## String Operators

| Operator | Meaning                        |
| -------- | ------------------------------ |
| `=`      | Equal                          |
| `!=`     | Not equal                      |
| `<`      | Sorts before lexicographically |
| `>`      | Sorts after lexicographically  |
| `-z`     | Is empty                       |
| `-n`     | Is not empty                   |

String ordering is affected by the current locale. The clearest way to use `<` and `>` is inside `[[ ]]`:

```bash theme={null}
[[ $a < $b ]]   # correct
```

They can also be used with `[ ]`, but the operators must be escaped so the shell does not treat them as redirections:

```bash theme={null}
[ "$a" \< "$b" ]
```

Inside `[ ]`, quoting variable expansions prevents word splitting and pathname expansion and safely handles empty values:

```bash theme={null}
[ "$name" = "snake" ]
```

Inside `[[ ]]`, quoting the left-hand variable is usually unnecessary because those expansions do not undergo word splitting or pathname expansion:

```bash theme={null}
[[ $name = "snake" ]]
```

## Integer Operators

| Operator | Meaning               |
| -------- | --------------------- |
| `-eq`    | Equal                 |
| `-ne`    | Not equal             |
| `-lt`    | Less than             |
| `-le`    | Less than or equal    |
| `-gt`    | Greater than          |
| `-ge`    | Greater than or equal |

## File Operators

These check properties of files and directories. We use them a lot in scripts that interact with the filesystem:

| Operator | Meaning                         |
| -------- | ------------------------------- |
| `-e`     | File exists                     |
| `-f`     | Is a regular file               |
| `-d`     | Is a directory                  |
| `-L`     | Is a symlink                    |
| `-N`     | Modified after last read        |
| `-O`     | Owned by the effective user ID  |
| `-G`     | Owned by the effective group ID |
| `-s`     | Size greater than 0             |
| `-r`     | Readable                        |
| `-w`     | Writable                        |
| `-x`     | Executable                      |

## Logical Operators

| Operator | Meaning |
| -------- | ------- |
| `!`      | NOT     |
| `&&`     | AND     |
| `\|\|`   | OR      |

We combine them to build more complex conditions. Example:

```bash theme={null}
if [[ -z ${1-} && ${#var} -gt 20 ]]
then
  do_something
fi
```

`${#var}` gives us the string length directly. Using `echo "$var" | wc -m` would also count the newline printed by `echo`.
