# Bash conditionals: [ ] and [[ ]] with an empty variable

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/bash-conditionals-full-en
[ ] is a POSIX test that follows argument rules; [[ ]] is a bash compound command that does pattern matching. Quoting is not tested here. In the run below the literal_no line holds only because the directory is empty at that point (files_before=0), so the glob a* has nothing to expand to.

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-57-A, TE-57-B and TE-57-C. The expected non-zero exit is TE-57-C (exit status 2). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-57-A: [ ] follows argument rules and [[ ]] is a bash compound command with pattern matching

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

The file written as `../name` sits one level above the working folder, inside the same throwaway tree.

Command:

```bash
echo files_before=$(ls -A | wc -l); x=abc; [ $x = abc ] && echo test_ok; [[ $x == a* ]] && echo pattern_ok; [ $x = a* ] && echo literal_match || echo literal_no; e=''; [ $e = a ] 2>../e.err; echo empty_exit=$?; grep -q 'unary operator' ../e.err && echo empty_msg=ok; echo files_after=$(ls -A | wc -l)
```

Recorded output:

```
files_before=0
test_ok
pattern_ok
literal_no
empty_exit=2
empty_msg=ok
files_after=0
```

Exit status: 0. This is the status recorded in the log for TE-57-A.

### TE-57-B: [ -f ] and [ -d ] test a file and a directory, -gt compares numbers, and [[ ]] matches a prefix pattern

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

Command:

```bash
touch x; [ -f x ] && echo file; [ -d x ] || echo notdir; n=10; [ "$n" -gt 9 ] && echo gt; [[ $n == 1* ]] && echo prefix
```

Recorded output:

```
file
notdir
gt
prefix
```

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

### TE-57-C: expected non-zero exit: [ ] with a number test on a word fails with status 2

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

Command:

```bash
[ 3 -gt abc ]
```

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-57-B: by the setup, x is a file. [ -f x ] was true, [ -d x ] was false so notdir was printed, [ "$n" -gt 9 ] was true for 10, and [[ $n == 1* ]] matched, which printed prefix. TE-57-C: [ 3 -gt abc ] exited with status 2, and standard error reads bash: line 1: [: abc: integer expression expected.

## Limits

The run uses an empty directory and the variables x (abc) and e (empty). It shows [ ] against a glob, pattern matching with [[ ]], and the error from an unquoted empty variable. Quoting is not tested here. TE-57-B runs -f, -d and -gt, and TE-57-C runs one failing numeric comparison. Other test operators are not tested.

## Source

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

## Evidence

- `TE-57-A`: [ ] follows argument rules and [[ ]] is a bash compound command with pattern matching

- 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-57-A` | [ ] follows argument rules and [[ ]] is a bash compound command with pattern matching | 0 | PASS | `338ed46e8c0d` |
| `TE-57-B` | [ -f ] and [ -d ] test a file and a directory, -gt compares numbers, and [[ ]] matches a prefix pattern | 0 | PASS | `fe4b8cd02faa` |
| `TE-57-C` | expected non-zero exit: [ ] with a number test on a word fails with status 2 | 2 | PASS | `2e60cbc58514` |

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 script arguments and functions: $1, $@, shift and return codes](https://kairos-2z6s.onrender.com/res/bash-arguments-functions-full-en)
- [Answer questions about a log file with a pipeline](https://kairos-2z6s.onrender.com/res/log-pipeline-full-en)
- [Bash exit codes: 0 is success, and && and || use it](https://kairos-2z6s.onrender.com/res/bash-exit-codes-full-en)
- [Bash variables, loops and if: the basics](https://kairos-2z6s.onrender.com/res/bash-variables-loops-full-en)
- [awk basics: split records into fields](https://kairos-2z6s.onrender.com/res/awk-basics-full-en)
