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

# Arrays and Flow Control

> Bash arrays, arithmetic, loops, pipelines, and case statements.

## Arrays

We define arrays using parentheses:

```bash theme={null}
domains=("google.com" "freyhm.com" "facebook.com")
```

To access an element by index:

```bash theme={null}
echo "${domains[0]}"   # google.com
```

The `${ }` with the index in square brackets is the syntax for element access — different from regular variable access. We quote the whole expansion so its value stays one argument.

## Arithmetic

Basic operators: `+`, `-`, `*`, `/`, `%` (modulus). We use `$(( ))` for arithmetic expressions:

```bash theme={null}
result=$((var1 + var2))
```

Parameter expansion can also give us the length of a variable's value:

```bash theme={null}
echo ${#variable}
```

## tee

`tee` reads from stdin and writes to stdout **and** to a file at the same time. Useful when we want to see output on screen while also saving it to a file:

```bash theme={null}
./script.sh | tee output.txt
```

## Loops

Bash has several loop forms. The three common list/condition forms below all use `do` / `done`.

**for** — iterates over a list:

```bash theme={null}
for var in 1 2 3 4
do
  echo "$var"
done
```

**while** — runs while the condition is **true**:

```bash theme={null}
while [ "$var" -eq 1 ]
do
  do_something
done
```

**until** — runs while the condition is **false** (the opposite of while):

```bash theme={null}
until [ "$counter" -eq 10 ]
do
  ((counter++))
done
```

## case

`case` is for branching without chaining a long list of `elif` statements. We match a value against patterns and run the corresponding block:

```bash theme={null}
case "$expression" in
  pattern) statement ;;
esac
```

A real example:

```bash theme={null}
case "$port" in
  80)  handle_http ;;
  443) handle_https ;;
  22)  handle_ssh ;;
  *)   : ;;   # catches anything else and does nothing
esac
```

`;;` closes each branch. `esac` closes the whole block. The `*` pattern is the catch-all — anything that didn't match above falls here. `:` is a command that does nothing successfully. If unknown values should terminate the whole script instead, we can use `exit` in that branch.
