Test Operators

Table 2.5 lists the operators that are used with test and the [...] construct. They can be logically combined with -a (“and”) and -o (“or”) and grouped with escaped parenthesis (\( ... \)). The string comparisons < and > are not available in versions of bash prior to 2.0.

Table B-5. Test Operators

Operator True If...
-b file file exists and is a block device file
-c file file exists and is a character device file
-d file file exists and is a directory
-e file file exists
-f file file exists and is a regular file
-g file file exists and has its setgid bit set
-G file file exists and is owned by the effective group ID
-k file file exists and has its sticky bit set
-L file file exists and is a symbolic link
-n string string is non-null
-O file file exists and is owned by the effective user ID
-p file file exists and is a pipe or named pipe (FIFO file)
-r file file exists and is readable
-s file file exists and is not empty
-S file file exists and is a socket
-t N File descriptor N points to a terminal
-u file file exists and has its setuid bit set
-w file file exists and is writeable
-x file

file exists and is executable, or file is a directory that can be searched

-z string string has a length of zero
fileA -nt fileB fileA is newer than fileB
fileA -ot fileB fileA is older than fileB
fileA -ef fileB

fileA and fileB point to the same file

stringA = stringB

stringA equals stringB

stringA != stringB stringA does not match stringB ...

Get Learning the bash Shell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.