Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash test operators

Reference for Bash test operators used in [ ] and [[ ]]: file tests like -f and -d, string tests like -z and -n, numeric comparisons and logical combinations.

Test operators are the small switches you use inside [ ... ], [[ ... ]] and the test command to ask a question about a file, a string or a number. The result is an exit status: 0 for true, 1 for false, and 2 for a usage error such as a missing operand.

The same operators appear in both bracket forms, with a few differences. This page lists them by group; the [[ ]] page covers the extras that only exist in double brackets (patterns, regular expressions, && and ||).

Terminal
[ -f "$file" ]            # file test
[ -n "$name" ]            # string test
[ "$a" -lt "$b" ]         # integer comparison

Try it

Bash File tests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash String, number and combined tests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]

File tests (the operand is a path):

Operator True if
-e the path exists
-f it is a regular file
-d it is a directory
-L, -h it is a symbolic link
-s it exists and its size is greater than zero
-r, -w, -x it is readable, writable, executable by you
-b, -c, -p, -S block device, character device, named pipe, socket
-u, -g, -k set-user-ID, set-group-ID, sticky bit is set
-O, -G owned by your effective user, group
f1 -nt f2, f1 -ot f2 f1 is newer, older than f2
f1 -ef f2 both refer to the same file (same device and inode)

String tests:

Operator True if
-z s the string is empty
-n s the string is not empty
s1 = s2, s1 == s2 the strings are equal (== is a Bash extension in [ ])
s1 != s2 the strings differ
s1 < s2, s1 > s2 sorts before, after (quote or escape < and > in [ ])

Number tests (integers only): -eq, -ne, -lt, -le, -gt, -ge.

Logic: ! expr negates; expr1 -a expr2 and expr1 -o expr2 are the old AND and OR for [ ] (avoid them); ( expr ) groups.

Usage notes

  • Inside [ ] always quote variables. An empty or multi-word unquoted variable changes the number of arguments and gives errors like "binary operator expected".
  • [ ] is an ordinary command (and a builtin), so spaces after [ and before ] are required. [[ ]] is shell syntax and avoids word splitting and glob expansion on its variables.
  • -e, -f, -d and -x follow symbolic links; -L and -h do not. A dangling link is -L true and -e false.
  • -r, -w and -x check what your effective user can do, not just the mode bits; root can read nearly anything.
  • With one argument, [ string ] is true if the string is non-empty, which is why [ "$x" ] works but [ -n ] (no operand) is also true. Prefer explicit -n and -z.
  • Use -a and -o rarely: their parsing is ambiguous with special values like ! or -n. Write [ a ] && [ b ] instead.
  • Floating-point numbers are not supported; use awk or bc for them.
  • -v name (set variable) and -R name (name reference) are Bash additions for [[ ]] and also work in [ ] in Bash 4.2+.