Learn the difference between [ and [[ in Bash, when to use each, and how to write if statements for strings, numbers, files and patterns without quoting bugs.
The short answer
#In a Bash script, use [[ ... ]] for tests. It is a Bash keyword, so it does not split words or expand globs inside the brackets, and it supports &&, ||, pattern matching and regular expressions. Use [ ... ] only when the script must run under plain sh. Always leave a space after the opening bracket and before the closing one.
_10name="Ada Lovelace"_10_10if [[ $name == "Ada Lovelace" ]]; then_10 echo "Hello, Ada"_10fi_10_10# POSIX version for /bin/sh: quote everything_10if [ "$name" = "Ada Lovelace" ]; then_10 echo "Hello, Ada"_10fi
Why it happens
#if in Bash does not take a condition the way it does in JavaScript. It runs a command and checks its exit status: zero means true, anything else means false. [ is an ordinary command (also called test), and ] is just its last argument. That is why the spaces matter: ["$a" = "b"] asks the shell to run a command literally named ["value", which fails with "command not found".
Because [ is a normal command, the shell expands its arguments before it runs. An unquoted variable that is empty disappears entirely, and one that contains spaces becomes several arguments. You get errors such as "unary operator expected" or "too many arguments".
[[ is part of Bash's syntax, so the shell parses what is inside it specially. Variables are not split or globbed, so [[ $name == "Ada Lovelace" ]] works even without quotes on the left. It also lets you combine conditions with && and || inside the brackets, which [ cannot do safely.
How to fix it
#Use [[ for strings and patterns
_11file="report.csv"_11_11if [[ -z $file ]]; then_11 echo "No file given"_11elif [[ $file == *.csv ]]; then_11 echo "CSV file"_11elif [[ $file =~ ^report-([0-9]+)\.txt$ ]]; then_11 echo "Report number ${BASH_REMATCH[1]}"_11else_11 echo "Something else"_11fi
On the right-hand side of ==, an unquoted value is treated as a glob pattern. Quote it when you want a literal comparison: [[ $a == "$b" ]]. For =~, keep the regex unquoted, or store it in a variable first, because quoting turns it into a plain string match.
Use (( )) for numbers
String comparison and number comparison are different operators. For integers, the arithmetic form is the easiest to read.
_10count=12_10_10if (( count > 10 )); then_10 echo "More than ten"_10fi_10_10# Equivalent test operators, which also work in [ ]_10if [[ $count -gt 10 ]]; then_10 echo "More than ten"_10fi
The test operators are -eq, -ne, -lt, -le, -gt and -ge. Inside [ the symbols for less than and greater than are redirections, so never use them for numbers there.
Test files with the same brackets
_10if [[ -f config.json && -r config.json ]]; then_10 echo "Config exists and is readable"_10fi_10_10if [[ ! -d build ]]; then_10 mkdir build_10fi
Use [ with quotes when you need POSIX sh
If the script starts with #!/bin/sh, or runs in a minimal container where sh is dash or BusyBox, [[ is not available. Quote every variable and combine tests with separate brackets.
_10#!/bin/sh_10if [ -n "$HOME" ] && [ -d "$HOME" ]; then_10 echo "Home is set"_10fi
Use = for string equality in [. The == form is a Bash extension.
Skip the brackets when you are testing a command
You do not need brackets to check whether a command worked. Put the command straight after if.
_10if grep -q "TODO" notes.txt; then_10 echo "Found a TODO"_10fi
Common pitfalls
#- Missing spaces.
if [[$x == 1]]fails. The brackets need a space on both sides. - Comparing numbers as strings.
[[ 10 == 10.0 ]]is false and[[ 9 > 10 ]]is true, because those operators compare text. Use(( ))or-gt. - Forgetting
thenorfi****. Putthenon the next line or after a semicolon:if ...; then. - Using
-aand-oinside[****. They are ambiguous and marked obsolescent in POSIX. Use[ a ] && [ b ]or[[ a && b ]]. - Running a Bash script with
sh****.sh script.shignores the Bash shebang, so[[may fail with "not found". Run it as./script.shorbash script.sh.
Related reading
#Was this resource helpful?