Linux Command Reference ยท Shell Builtins
Linux test command
Reference for test, [ ] and [[ ]] in Bash: file tests, string and integer comparisons, logical operators, and the differences between the three forms.
test evaluates a condition and reports the result through its exit status: 0 means true, 1 means false, and 2 means the expression itself was malformed. It prints nothing. Every if, while and && in a script ultimately rests on an exit status, and test is the command that turns questions such as "does this file exist?" or "is this number larger than that one?" into one.
There are three spellings. test EXPR and [ EXPR ] are the same builtin (the closing ] is just a required last argument), and they work in any POSIX shell. [[ EXPR ]] is a Bash keyword with its own parser: it is safer with unquoted variables and adds pattern and regex matching.
test -f notes.txt && echo "file exists" [ "$count" -gt 10 ] && echo "more than ten" [[ $name == a* ]] && echo "starts with a"
Try it
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.
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
test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]
The most useful primaries:
| Name | Description |
|---|---|
-e F |
F exists (any type). |
-f F, -d F, -L F |
Regular file, directory, symbolic link. |
-r F, -w F, -x F |
Readable, writable, executable by the current user. |
-s F |
Exists and has a size greater than zero. |
-nt, -ot, -ef |
A -nt B: A is newer; -ot: older; -ef: same file (same device and inode). |
-z S, -n S |
String is empty, string is not empty. |
S1 = S2, S1 != S2 |
String equality and inequality (== also works in Bash). |
N1 -eq N2 and -ne -lt -le -gt -ge |
Integer comparison. |
-v NAME |
Variable NAME is set (Bash). |
! E, E1 -a E2, E1 -o E2 |
Not, and, or. |
( E ) |
Grouping (escape the parentheses inside [ ]). |
Returns: 0 if the expression is true, 1 if false, 2 on a syntax error (for example a non-numeric operand to -gt).
Edge cases: with one argument, test STRING is true when the string is non-empty, so [ "$x" ] works but [ $x ] breaks when x is empty or contains spaces.
Usage notes
- Spaces are mandatory.
[ -f file ]needs a space after[and before].[-f file]is a command named[-f. - Quote variables inside
[ ]. The shell splits and globs unquoted expansions before[sees them, so[ $a = b ]fails whenais empty. Inside[[ ]]quoting is not needed on the left side. =compares strings,-eqcompares numbers.[ 7 = 07 ]is false,[ 7 -eq 07 ]is true. Inside[[ ]],<and>compare strings; for numeric order use-lt/-gtor(( a < b )).- Do not use
<and>inside[ ]unescaped. They are redirections. Write\<or switch to[[ ]]. - Avoid
-aand-o. Their parsing is ambiguous with some operands; chain separate tests with&&and||instead. [[ ]]is Bash, not POSIX. Scripts run bysh(dash) must use[ ].- Regex and glob on the right of
[[ ]]: an unquoted right side of==is a glob; a quoted one is literal.=~takes an extended regular expression; keep it in a variable when it is complicated. [is also a real program (/usr/bin/[), but the builtin is used first. Usetypeto see which one runs.- Numbers only support integers. For decimals use
bcorawk.
Related
trueandfalse,read,set,let, Test operators,[[ ]],if- Lessons: Conditionals, Exit codes and errors, Arithmetic