# Bash variables, loops and if: the basics

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/bash-variables-loops-full-en
Shell variables hold strings; unquoted expansion splits on whitespace; `if` runs a command and tests its exit status.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-36-A, TE-36-B and TE-36-C. The expected non-zero exit is TE-36-C (exit status 2). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-36-A: an unquoted variable word-splits; quoting preserves one argument

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
v='a b'; printf 'unquoted=%s\n' $(echo $v | wc -w); printf 'quoted=%s\n' "$v"; set -- $v; echo args=$#; set -- "$v"; echo args_quoted=$#
```

Recorded output:

```
unquoted=2
quoted=a b
args=2
args_quoted=1
```

### TE-36-B: a while loop repeats until its test fails, and a for loop walks a list of words

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
n=0; while [ $n -lt 3 ]; do n=$((n+1)); done; echo n=$n; for w in a b c; do printf '%s,' $w; done; echo
```

Recorded output:

```
n=3
a,b,c,
```

Exit status: 0. This status and the output match what was written before the run.

### TE-36-C: expected non-zero exit: a numeric test on text that is not a number fails with exit status 2

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
v=abc; [ "$v" -gt 5 ]
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
bash: line 1: [: abc: integer expression expected
```

Exit status: 2. This status and the output match what was written before the run.

TE-36-B: the while loop stopped when n reached 3, and the for loop printed a, b and c with a comma after each. TE-36-C: by the setup, v is the text abc. The test [ "$v" -gt 5 ] exited with status 2, and bash printed bash: line 1: [: abc: integer expression expected to standard error.

## Limits

TE-36-A covers quoting, TE-36-B a while loop and a for loop, and TE-36-C one failing numeric test. Arrays, `set -u` and changes to `IFS` are outside this page.

## Source

- https://www.gnu.org/software/bash/manual/bash.html

## Evidence

- `TE-36-A`: an unquoted variable word-splits; quoting preserves one argument

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-36-A` | an unquoted variable word-splits; quoting preserves one argument | 0 | PASS | `df12867b0e62` |
| `TE-36-B` | a while loop repeats until its test fails, and a for loop walks a list of words | 0 | PASS | `290906c0a141` |
| `TE-36-C` | expected non-zero exit: a numeric test on text that is not a number fails with exit status 2 | 2 | PASS | `94091b687f1b` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Bash exit codes: 0 is success, and && and || use it](https://kairos-2z6s.onrender.com/res/bash-exit-codes-full-en)
- [find: -maxdepth, -type f and -size +1c narrow the result](https://kairos-2z6s.onrender.com/res/find-options-full-en)
- [mkdir, cp, mv and rm: the basic file commands](https://kairos-2z6s.onrender.com/res/file-commands-full-en)
- [chmod: read the permission string and change it](https://kairos-2z6s.onrender.com/res/chmod-basics-full-en)
- [Create and switch to a new Git branch](https://kairos-2z6s.onrender.com/res/create-and-switch-git-branch-en)
- [Bash conditionals: [ ] and [[ ]] with an empty variable](https://kairos-2z6s.onrender.com/res/bash-conditionals-full-en)
