LinuxCommandLibrary

test

Evaluate conditional expressions

TLDR

Test if a given variable is equal to a given string

$ test "[$MY_VAR]" = "[/bin/zsh]"
copy

Test if a given variable is empty ([z]ero length)
$ test -z "[$GIT_BRANCH]"
copy

Test if a [f]ile exists
$ test -f "[path/to/file_or_directory]"
copy

Test if a [d]irectory does not exist
$ test ! -d "[path/to/directory]"
copy

If A is true, then do B, or C in the case of an error (notice that C may run even if A fails)
$ test [condition] && [echo "true"] || [echo "false"]
copy

SYNOPSIS

test expression
[ expression ]

PARAMETERS

-b file
    True if file exists and is a block special file.

-c file
    True if file exists and is a character special file.

-d file
    True if file exists and is a directory.

-e file
    True if file exists (regardless of type).

-f file
    True if file exists and is a regular file.

-g file
    True if file exists and has its set-group-ID bit set.

-G file
    True if file exists and is owned by the effective group ID.

-h file or -L file
    True if file exists and is a symbolic link.

-k file
    True if file exists and has its sticky bit set.

-n string
    True if the length of string is non-zero.

-o option
    True if shell option option is enabled.

-O file
    True if file exists and is owned by the effective user ID.

-p file
    True if file exists and is a named pipe (FIFO).

-r file
    True if file exists and is readable.

-s file
    True if file exists and has a size greater than zero.

-S file
    True if file exists and is a socket.

-t fd
    True if file descriptor fd is open and refers to a terminal.

-u file
    True if file exists and has its set-user-ID bit set.

-w file
    True if file exists and is writable.

-x file
    True if file exists and is executable.

-z string
    True if the length of string is zero.

string1 = string2
    True if the strings string1 and string2 are equal.

string1 != string2
    True if the strings string1 and string2 are not equal.

integer1 -eq integer2
    True if integer1 is equal to integer2.

integer1 -ge integer2
    True if integer1 is greater than or equal to integer2.

integer1 -gt integer2
    True if integer1 is greater than integer2.

integer1 -le integer2
    True if integer1 is less than or equal to integer2.

integer1 -lt integer2
    True if integer1 is less than integer2.

integer1 -ne integer2
    True if integer1 is not equal to integer2.

! expression
    True if expression is false.

expression1 -a expression2
    True if both expression1 and expression2 are true.

expression1 -o expression2
    True if either expression1 or expression2 is true.

DESCRIPTION

The test command in Linux evaluates conditional expressions. It checks file types, compares strings, and performs arithmetic comparisons. Its primary purpose is to determine whether a condition is true or false and sets an exit status code accordingly. An exit status of 0 signifies that the condition is true, and a non-zero exit status indicates false. test is heavily used in shell scripts for conditional logic, controlling the flow of execution based on whether certain criteria are met. Its short-form alias is [, which requires a closing ]. The command operates on file existence and characteristics (readable, writable, executable), string comparisons (equality, inequality, null), integer comparisons (greater than, less than, equal), and logical operations (AND, OR, NOT). The test command does not print output, instead it sets the $? variable.

CAVEATS

Using unquoted variables in test can lead to unexpected behavior if the variable contains whitespace or special characters. Always quote variables to avoid word splitting and globbing.
Be careful when comparing strings and integers. Use the appropriate operators (-eq, -ne, -gt, -lt, -ge, -le for integers; = and != for strings).
The [ command requires a space after the opening bracket and before the closing bracket.

EXIT STATUS

The test command returns an exit status that indicates the result of the expression. A return value of 0 means the expression is true, and a non-zero value (typically 1) means it is false. This exit status is crucial for using test in conditional statements within shell scripts. The value of $? contains the return value.

PORTABILITY

The test command is highly portable across Unix-like systems due to its POSIX specification. Shell scripts that use standard test constructs are likely to behave consistently regardless of the underlying operating system.

HISTORY

The test command is a fundamental utility found in virtually all Unix-like systems. It has been a part of the Unix toolset since its early days and is specified by POSIX. The command's simplicity and utility in shell scripting made it an integral part of automated system administration and application development. Its evolution has primarily focused on standardization and ensuring consistent behavior across different Unix and Linux distributions. The alias [ has been present since early versions of Bourne shell. The command is a vital piece in shell scripting languages.

SEE ALSO

if(1), find(1), bash(1)

Copied to clipboard