On Unix-like operating systems, the test command checks file types and compares values.

This page covers the GNU/Linux version of test.

Description

test is used as part of the conditional execution of shell commands.

For information about the test command in bash see our bash test command page.

  • Description
  • Syntax
  • Expressions
  • Examples
  • Linux commands help

test exits with the status determined by EXPRESSION. Placing the EXPRESSION between square brackets ([ and ]) is the same as testing the EXPRESSION with test. To see the exit status at the command prompt, echo the value “$?” A value of 0 means the expression evaluated as true, and a value of 1 means the expression evaluated as false.

Syntax

test EXPRESSION

[ EXPRESSION ]

Expressions

Expressions take the following forms:

Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be escaped (e.g., by backslashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of STRING.

NOTE: your shell may have its own version of test, which usually supersedes the version described here. Please refer to your shell’s documentation for details about the options it supports.

Examples

test 100 -gt 99 && echo “Yes, that’s true.” || echo “No, that’s false.”

This command prints the text “Yes, that’s true.” because 100 is greater than 99.

test 100 -lt 99 && echo “Yes.” || echo “No.”

This command prints the text “No.” because 100 is not less than 99.

[ “awesome” = “awesome” ]; echo $?

This command prints “0” because the expression is true; the two strings are identical.

[ 5 -eq 6 ]; echo $?

This command prints “1” because the expression is false; 5 does not equal 6.