🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

test Command

Intermediate Shell Scripting man(1)

Evaluate conditional expressions for scripting

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
test EXPRESSION

What Does test Do?

test evaluates conditional expressions and returns an exit status of 0 (true) or 1 (false). It is the foundation of if statements and conditional logic in shell scripts.

test can be written as test expression or [ expression ] (the bracket syntax). Bash also provides [[ expression ]] which is more powerful and safer (it handles word splitting and glob expansion better).

test supports file tests (-f, -d, -r, -w, -x), string comparisons (=, !=, -z, -n), numeric comparisons (-eq, -ne, -lt, -gt), and logical operators (-a, -o, !).

Options & Flags

OptionDescriptionExample
-f True if file exists and is a regular file test -f /etc/passwd && echo "exists"
-d True if directory exists [ -d /var/log ] && echo "exists"
-e True if file exists (any type) [ -e /path/to/file ]
-r/-w/-x True if file is readable/writable/executable [ -x /usr/bin/python3 ]
-z True if string is empty [ -z "$VAR" ] && echo "empty"
-n True if string is non-empty [ -n "$NAME" ] && echo "set"
-eq/-ne/-lt/-gt Numeric comparisons [ "$count" -gt 10 ] && echo "more than 10"
= String equality [ "$answer" = "yes" ]

Practical Examples

#1 Check if file exists

Tests if config.yml exists and is a regular file.
$ [ -f config.yml ] && echo "Config found" || echo "Config missing"

#2 Check directory

Tests if the directory exists.
$ [ -d /var/www ] && echo "Directory exists"

#3 Numeric comparison

Compares numbers using -gt (greater than).
$ count=15; [ $count -gt 10 ] && echo "More than 10"
Output: More than 10

#4 String comparison

Checks if the current user is root.
$ [ "$USER" = "root" ] && echo "Running as root"

#5 Empty string check

Tests if a variable is empty or unset.
$ [ -z "$API_KEY" ] && echo "API_KEY not set!"

#6 Complex condition

Checks if file exists AND is executable using bash [[ syntax.
$ [[ -f app.py && -x app.py ]] && echo "Executable Python script"

#7 If statement

Uses test in an if statement for conditional logic.
$ if [ -r /etc/shadow ]; then echo "Can read shadow"; else echo "No access"; fi

Tips & Best Practices

Always quote variables: Use [ "$var" = "value" ] with quotes. Without quotes, empty variables cause syntax errors: [ = value ] fails.
[[ vs [ in bash: [[ is bash-specific but safer — it handles empty variables and supports regex (=~) and glob (*) matching. Prefer [[ in bash scripts.
Spaces are required: [ and ] need spaces: [ -f file ] is correct. [-f file] or [ -f file] will fail. The brackets are actually commands.

Frequently Asked Questions

How do I check if a file exists in bash?
Use [ -f filename ] for regular files, [ -d dirname ] for directories, [ -e path ] for any type. Always quote variables.
How do I compare numbers in bash?
Use -eq (equal), -ne (not equal), -lt (less than), -gt (greater than), -le (less or equal), -ge (greater or equal).
What is the difference between [ ] and [[ ]]?
[ ] is POSIX-standard (works everywhere). [[ ]] is bash-specific but supports regex, glob, and handles empty variables better.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →