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