---
title: "[ vs [[ in Bash: how do I write an if statement correctly?"
description: "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."
url: https://www.danny.engineering/answer/bash/vsin-bash-how-do-i-write-an-if-statement-correctly
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:31:00.000Z
tags: ["Bash"]
---

# [ vs [[ in Bash: how do I write an if statement correctly?

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


```bash
name="Ada Lovelace"

if [[ $name == "Ada Lovelace" ]]; then
  echo "Hello, Ada"
fi

# POSIX version for /bin/sh: quote everything
if [ "$name" = "Ada Lovelace" ]; then
  echo "Hello, Ada"
fi
```


## 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


```bash
file="report.csv"

if [[ -z $file ]]; then
  echo "No file given"
elif [[ $file == *.csv ]]; then
  echo "CSV file"
elif [[ $file =~ ^report-([0-9]+)\.txt$ ]]; then
  echo "Report number ${BASH_REMATCH[1]}"
else
  echo "Something else"
fi
```


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.


```bash
count=12

if (( count > 10 )); then
  echo "More than ten"
fi

# Equivalent test operators, which also work in [ ]
if [[ $count -gt 10 ]]; then
  echo "More than ten"
fi
```


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


```bash
if [[ -f config.json && -r config.json ]]; then
  echo "Config exists and is readable"
fi

if [[ ! -d build ]]; then
  mkdir build
fi
```


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


```bash
#!/bin/sh
if [ -n "$HOME" ] && [ -d "$HOME" ]; then
  echo "Home is set"
fi
```


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


```bash
if grep -q "TODO" notes.txt; then
  echo "Found a TODO"
fi
```


## 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** **`then`** **or** **`fi`****.** Put `then` on the next line or after a semicolon: `if ...; then`.
- **Using** **`-a`** **and** **`-o`** **inside** **`[`****.** They are ambiguous and marked obsolescent in POSIX. Use `[ a ] && [ b ]` or `[[ a && b ]]`.
- **Running a Bash script with** **`sh`****.** `sh script.sh` ignores the Bash shebang, so `[[` may fail with "not found". Run it as `./script.sh` or `bash script.sh`.

## Related reading

- [GNU Bash manual: Conditional constructs](https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html)
- [GNU Bash manual: Bash conditional expressions](https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html)
