Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Syntax

#!/usr/bin/env bash
config="/etc/nginx/nginx.conf"
if [[ -f "$config" ]]; then
    echo "Config found"
elif [[ -d /etc/nginx ]]; then
    echo "Folder exists, config missing"
else
    echo "Nginx is not installed"
fi

Spaces inside the brackets are required: [[-f "$config"]] is an error. Every if ends with fi, and elif and else are optional.

[ ] vs [[ ]]

[ is an ordinary command, also called test, that exists in every POSIX shell. [[ is part of Bash itself. The differences that matter:

  • Word splitting: inside [[ ]] an unquoted empty variable does not break the test. With [ ] you must quote every variable.
  • Combining: [[ ]] accepts && and || inside the brackets.
  • Patterns: [[ ]] supports wildcard matching with == and regular expressions with =~.

In Bash scripts, use [[ ]] throughout. Use [ ] only in scripts that must run under /bin/sh.

File Tests

TestTrue when the path
-eexists
-fis a regular file
-dis a directory
-sis not empty
-r / -w / -xis readable / writable / executable

Put ! in front to negate: [[ ! -d "$dir" ]].

Strings

#!/usr/bin/env bash
env_name="${1:-}"
if [[ -z "$env_name" ]]; then
    echo "No environment given" >&2
    exit 1
fi
if [[ "$env_name" == "production" ]]; then
    echo "Deploying to production"
fi
if [[ "$env_name" == stag* ]]; then
    echo "Starts with stag"
fi
  • -z: the string is empty. -n: it is not empty.
  • == and !=: equal and not equal. An unquoted right side is a wildcard pattern.
  • =~: matches a regular expression, for example [[ "$ip" =~ ^[0-9.]+$ ]].

Numbers

Use the letter operators for numbers. < and > inside [[ ]] compare text alphabetically, so 10 sorts before 9.

OperatorMeaning
-eq / -neequal / not equal
-lt / -leless than / or equal
-gt / -gegreater than / or equal
#!/usr/bin/env bash
usage="$(df --output=pcent / | tail -n 1 | tr -dc '0-9')"
if [[ "$usage" -ge 90 ]]; then
    echo "Root disk is ${usage}% full"
fi
if (( usage > 80 )); then
    echo "Over 80%"
fi

Inside (( )), > is a numeric comparison. More on disk checks is in how to check disk space in Linux.

Combine Conditions

#!/usr/bin/env bash
file="/var/www/example.com/.env"
if [[ -f "$file" && -r "$file" ]]; then
    echo "Readable"
fi
if [[ "$USER" == "root" || "$USER" == "deploy" ]]; then
    echo "Allowed user"
fi

Test a Command's Exit Status

Any command can be the condition. Its exit status decides:

#!/usr/bin/env bash
if grep -q "^deploy:" /etc/passwd; then
    echo "User deploy exists"
fi
if ! command -v git > /dev/null; then
    echo "Git is not installed" >&2
    exit 1
fi

For one-line checks, && runs the next command only on success and || only on failure: [[ -d "$dir" ]] || mkdir -p "$dir".

Common Mistakes

  • Using = for numbers: it compares text; "05" == "5" is false. Use -eq.
  • Missing then or fi: check with bash -n script.sh.
  • Testing $? after another command: test the command directly in the if.

Use conditions inside for loops, and start from how to write a Bash script if you are new to scripts.

Was this answer helpful?
Loading...