KKAIROS
Practical guideIllustrative4 min read

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

[ ] 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.

Published:

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.

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:

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:

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:

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

Ad

Source

Evidence

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.

All full guides: All guides