Skip to content

Test Commands

Test commands are special commands built into the language that allows you to perform conditional expressions. There are three test commands:

  • [[ ... ]]: inspired from bash and works by wrapping a conditional expression within a pair of brackets like this: [[ <expression> ]].
  • [...]: works exactly the same as previous command except that it uses the operators -a and -o instead of && and || respectively. (we will learn more about expressions below).
  • test: works by putting a conditional expression in front of the test keyword. for example: test <expression>. Has same rules as [...].

The exit code of conditional commands is zero if the expression evaluates to true. and one otherwise.

TIP

Unlike bash, there is no need to escape the special characters in expressions when using the test or [...] commands. Enjoy writing code like this: [ $foo < $bar ] 🎉 .

Expressions

A conditional expression can be devided into two categories. Unary expressions and binary expressions.

Unary expressions

The format is :

sh
<FLAG> word

The <FLAG> dictates what type of test you want to perform. The following table lists all possible flags with the their functions. The word can be any valid expression the expands to a string, and is the operand over which you want to perform the test.

FlagDescriptionExample
-aTrue if file exists.[ -a /path/to/file ]
-bTrue if file exists and is a block special file.[ -b /dev/sda ]
-cTrue if file exists and is a character special file.[ -c /dev/tty ]
-dTrue if file exists and is a directory.[ -d /home/user ]
-eTrue if file exists.[ -e /path/to/file ]
-fTrue if file exists and is a regular file.[ -f /etc/passwd ]
-gTrue if file exists and its set-group-id bit is set.[ -g /path/to/file ]
-hTrue if file exists and is a symbolic link.[ -h /path/to/symlink ]
-kTrue if file exists and its "sticky" bit is set.[ -k /tmp ]
-pTrue if file exists and is a named pipe (FIFO).[ -p /path/to/pipe ]
-rTrue if file exists and is readable.[ -r /path/to/file ]
-sTrue if file exists and has a size greater than zero.[ -s /path/to/file ]
-tTrue if file descriptor is open and refers to a terminal.[ -t 1 ]
-uTrue if file exists and its set-user-id bit is set.[ -u /path/to/file ]
-wTrue if file exists and is writable.[ -w /path/to/file ]
-xTrue if file exists and is executable.[ -x /path/to/script.sh ]
-GTrue if file exists and is owned by the effective group ID.[ -G /path/to/file ]
-LTrue if file exists and is a symbolic link.[ -L /path/to/symlink ]
-NTrue if file exists and has been modified since it was last read.[ -N /path/to/file ]
-OTrue if file exists and is owned by the effective user ID.[ -O /path/to/file ]
-STrue if file exists and is a socket.[ -S /var/run/docker.sock ]
-vTrue if the shell variable is set (has been assigned a value).[ -v MY_VAR ]
-zTrue if the length of string is zero.[ -z $EMPTY_VAR ]
-nTrue if the length of string is non-zero.[ -n $MY_VAR ]

There is an alternative syntax for the -n flag. You can omit the flag at all and only keep the operand. for example [ $var ] returns true if the length of the value of $var is non-zero.

Binary expressions

The format is:

sh
word1 <FLAG> word2

Binary expressions are used for comparison between two operands. The <FLAG> dictates what type of comparison you want to perform. The following table lists all possible flags with the their functions. word1 and word2 can be any valid expressions the expand to string, and are the operands over which you want to perform the comparison.

ExpressionDescription
file1 -ef file2True if file1 and file2 refer to the same device and inode numbers.
file1 -nt file2True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
file1 -ot file2True if file1 is older than file2, or if file2 exists and file1 does not.
string1 = string2True if string1 is equal to string2 (POSIX-compliant form).
string1 == string2True if string1 is equal to string2 (performs pattern matching with [[...]] command).
string1 != string2True if string1 is not equal to string2.
string1 < string2True if string1 sorts before string2 lexicographically.
string1 > string2True if string1 sorts after string2 lexicographically.
num1 -eq num2True if num1 is equal to num2.
num1 -ne num2True if num1 is not equal to num2.
num1 -lt num2True if num1 is less than num2.
num1 -le num2True if num1 is less than or equal to num2.
num1 -gt num2True if num1 is greater than num2.
num1 -ge num2True if num1 is greater than or equal to num2.

Combining Expressions

One test command can contain exactly one conditional expression. However, if you want to use more expressions, then you should use expression combination.

Expressions may be combined using the following operators, listed in increasing order of precedence:

Logical OR:

The format is:

sh
expression1 -o expression2

Or if used with [[...]] command

sh
expression1 || expression2

True if either expression1 or expression2 is true. expression2 is not evaluated if expression1 evaluates to true.

Logical AND:

The format is:

sh
expression1 -a expression2

Or if used with [[...]] command

sh
expression1 && expression2

True if both expression1 and expression2 are true. expression2 is not evaluated if expression1 evaluates to false.

Expression Inversion:

The format is:

sh
! expression

True if expression is false. And false otherwise.

Grouping:

The format is:

sh
( expression )

Returns the value of expression. This may be used to override the normal precedence of operators.

TIP

You are not limited to one combination expression at a time. You can combine them as well to create complex conditional expressions. for example: ! (-f file && -n string)

Released under the 3-Clause BSD License.