LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

test

Evaluate conditional expressions

TLDR

Check if a file exists
$ test -e [path/to/file] && echo "exists"
copy
Check if file is a directory
$ test -d [path/to/dir] && echo "is directory"
copy
Check if file is readable
$ test -r [path/to/file] && echo "readable"
copy
Compare strings for equality
$ test "[string1]" = "[string2]"
copy
Check if string is non-empty
$ test -n "[string]"
copy
Compare integers
$ test [5] -gt [3] && echo "greater"
copy
Combine conditions with AND
$ test -f [file] -a -r [file]
copy
Combine conditions with OR
$ test -f [file1] -o -f [file2]
copy

SYNOPSIS

test EXPRESSION[ EXPRESSION ]

DESCRIPTION

test evaluates conditional expressions, returning exit status 0 (true) or 1 (false). It is commonly used in shell scripts for decision-making in if statements and loops.The command can also be invoked as [ with a closing ] required as the last argument. This bracket syntax is more readable in conditionals: if [ -f file ]; then.Modern shells also provide [[ which offers additional features like pattern matching and safer string handling, but is not POSIX-compliant.

FILE TESTS

-e FILE

True if file exists.
-f FILE
True if file exists and is a regular file.
-d FILE
True if file exists and is a directory.
-r FILE
True if file is readable.
-w FILE
True if file is writable.
-x FILE
True if file is executable.
-s FILE
True if file exists and has size greater than zero.
-L FILE
True if file is a symbolic link.
-b FILE
True if file is a block special file.
-c FILE
True if file is a character special file.
-p FILE
True if file is a named pipe (FIFO).
-S FILE
True if file is a socket.
-g FILE
True if file has set-group-ID bit set.
-u FILE
True if file has set-user-ID bit set.
-O FILE
True if file is owned by the effective user ID.
FILE1 -nt FILE2
True if FILE1 is newer (modification date) than FILE2.
FILE1 -ot FILE2
True if FILE1 is older than FILE2.
FILE1 -ef FILE2
True if FILE1 and FILE2 refer to the same device and inode numbers.

STRING TESTS

-n STRING

True if string length is non-zero.
-z STRING
True if string length is zero.
STRING1 = STRING2
True if strings are equal.
STRING1 != STRING2
True if strings are not equal.

INTEGER COMPARISONS

INT1 -eq INT2

Equal.
INT1 -ne INT2
Not equal.
INT1 -lt INT2
Less than.
INT1 -le INT2
Less than or equal.
INT1 -gt INT2
Greater than.
INT1 -ge INT2
Greater than or equal.

OPERATORS

! EXPR

True if expression is false.
EXPR1 -a EXPR2
True if both expressions are true (AND).
EXPR1 -o EXPR2
True if either expression is true (OR).

CAVEATS

Variables in test expressions should be quoted to handle empty values and spaces correctly. The [ form requires spaces around brackets. String comparison uses = not == for POSIX compliance. Integer comparison operators (-eq, -lt) are different from string operators.

HISTORY

test is one of the original Unix utilities, dating back to Version 7 Unix in 1979. The bracket notation [ was added as an alias for improved readability in shell scripts. Both forms are specified by POSIX and available as shell builtins and standalone commands.

SEE ALSO

bash(1), sh(1), expr(1)

Copied to clipboard
Kai