$_ CLI In Depth
Learning progress 0%
lesson scripting exit-codes control-flow

test, [ ] and [[ ]]

6 min read Module 13: Scripting Basics & Exit Codes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:24

A condition is just a command

In shell, a condition is not a special expression. It is a command, and the if statement uses its exit status: 0 means true and anything else means false. The command grep -q root /etc/passwd is a condition in exactly this sense, because it exits with 0 when it finds a match. The most common condition command is test, which checks files, strings, and numbers.

The bracket is a command too

The test command has a second name. The bracket [ is a builtin that behaves like test, except that it requires a closing argument, ]. The two forms below do the same thing, and you will see both in real scripts:

BASH2 lines
test -f notes.txt
[ -f notes.txt ]

The spaces after [ and before ] are required, because the bracket and the closing bracket are separate words. Written without spaces, [-f notes.txt] is a single unknown command name, and bash reports that it cannot find it.

Each test form is a single command, so the spacing and the operand count matter. A test with one operand checks whether that operand is non-empty. A test with two operands, such as a unary operator followed by a path, uses that operator. A test with three operands, such as a value, an operator, and a second value, compares them. Count the operands before you read the operator, and most test mistakes become obvious.

Files, strings and numbers

File tests take one operand. The option -e is true when the path exists, -f when it is a regular file, and -d when it is a directory. The options -r, -w, and -x check read, write, and execute permission for the current user. The option -s is true when the file exists and is not empty. Read the test names as short sentences: if the file exists, if it is a directory, and so on.

String tests compare text. The option -z is true when the string is empty, and -n is true when it is not empty. The equals sign compares two strings, and != checks that they differ. Bash also accepts == in the single bracket form, but that spelling is not standard there, so use a single equals sign when you want a portable test.

Number tests use words, not symbols, in the single bracket form. The options -eq, -ne, -lt, -le, -gt, and -ge compare two integers. The test [ 10 -lt 9 ] is false, because it compares the numbers. Using the equals sign for numbers compares text instead, which gives the wrong answer for values such as 010 and 10.

Why quoting still matters

A test sees its operands after expansion, so an empty variable can remove an operand and change the meaning of the whole test. Consider the common mistake below. When name is empty, the test becomes [ -n ], and a single operand that is not empty counts as true.

BASH2 lines
name=""
[ -n $name ] && echo "has a name"

That command prints the message even though the variable is empty. Quoting the variable, as in [ -n "$name" ], keeps the empty string as an operand, so the test returns false as intended. The rule is simple: quote every variable inside a test, and quote every operand that might contain a space.

The double-bracket form

The double-bracket form [[ ... ]] is a keyword rather than a command, and bash parses it with its own rules. Inside it, operands are not split into words and are not expanded as file names, so quotes around a variable are optional. It also accepts && and || inside the brackets, and it can compare strings with < and > without escaping. In the single bracket form those symbols are redirections, so they must be escaped.

String comparison inside double brackets is lexical. The expression [[ 10 < 9 ]] is true, because 10 sorts before 9 as text. For numbers, use arithmetic with double parentheses or the numeric options, which compare values. Mixing the two is a common source of wrong results in scripts. The double-bracket form is not part of POSIX sh, so use it only in scripts that start with a bash shebang.

The double-bracket form also has no special treatment for an empty operand on the left. The expression [[ -z $name ]] is correct even when name is unset, because the keyword sees a single operand that is replaced by nothing, and it reports the empty string correctly. This is the main practical reason to prefer double brackets in new bash scripts.

Patterns and regular expressions

On the right side of == inside double brackets, an unquoted word is a glob pattern. The test [[ $file == *.log ]] is true for any name that ends in .log. Quoting the pattern makes it a literal string. On the right side of =~, the operand is a POSIX extended regular expression. Store the expression in a variable first, and leave it unquoted in the test, because quotes would turn it into a literal string:

BASH3 lines
re='^[0-9]+$'
n=42
[[ $n =~ $re ]] && echo "number"

Text captured by a regular expression appears in the BASH_REMATCH array, which a later module covers along with arrays.

Hands-on: test your conditions

Run the lines below one at a time and predict each result before you look. The first two lines use a quoted or double-bracket test, and they print a message only when the test is true. The third uses an unquoted operand, which breaks when the value is empty.

BASH4 lines
name=""
[ -z "$name" ] && echo "empty, quoted"
[[ -z $name ]] && echo "empty, double bracket"
[ -n $name ] && echo "unquoted single bracket says non-empty"

The third line prints a message even though the variable is empty, which is the bug this unit warns about. When a test fails for no clear reason, run it with set -x first. Bash then prints each command with its expanded operands, and you can see exactly what the test received. Turn the trace off again with set +x.

Terminal lab · 2 steps · real Linux in your browser

Test files, strings and patterns

Classify paths with file tests, then match names with a double-bracket pattern.

01
Classify a path
Create kind.sh in /workspace. It takes one path and prints one word. It prints missing when nothing exists at the path, directory when the path is a directory, file when it is a regular file, and other for anything else. Use test operators such as -e, -d, and -f, and quote the argument so that a path with a space works. Put the missing check first. The other file tests are false for a path that does not exist, so order matters less than you might expect, but the missing case still reads best first.
Show hint for step 1
Use an if with elif branches, and quote every use of $1 inside the test.
02
Match a name with a pattern
Create match.sh in /workspace. It prints log when its first argument ends in .log, and other when it does not. Use the double-bracket form with an unquoted pattern on the right of ==, so the argument is matched as a glob. The script must work for a name that contains a space and for an empty argument. Test it with my app.log, with notes.txt, and with an empty string.
Show hint for step 2
Inside double brackets an unquoted word on the right of == is a glob pattern. The word $1 can stay unquoted there, because double brackets do not split it.
Check yourself Question 1 of 4
Concept Check Test for an empty string recall

Which test operator is true when a string is empty?

Recap & Key Takeaways

A condition is a command, and test or [ ] returns 0 when the check is true, while [[ ]] is a keyword with its own grammar.
Quote variables inside a single bracket test, because an empty unquoted operand can remove an argument and change the meaning of the test.
Numeric options such as -lt compare numbers, while < and = inside [[ ]] compare strings.
Use =~ with a regular expression stored in a variable, and use an unquoted right side of == for a glob pattern.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue