11. Conditional Expressions

A conditional expression is used with the [[
compound command to test attributes of files and to compare strings.
Each expression can be constructed from one or more
of the following unary or binary expressions:

-afile

true if file exists.

-bfile

true if file exists and is a block special file.

-cfile

true if file exists and is a character special file.

-dfile

true if file exists and is a directory.

-efile

true if file exists.

-ffile

true if file exists and is a regular file.

-gfile

true if file exists and has its setgid bit set.

-hfile

true if file exists and is a symbolic link.

-kfile

true if file exists and has its sticky bit set.

-nstring

true if length of string is non-zero.

-ooption

true if option named option is on. option
may be a single character, in which case it is a single letter option name.
(See 15.1 Specifying Options.)

-pfile

true if file exists and is a FIFO special file (named pipe).

-rfile

true if file exists and is readable by current process.

-sfile

true if file exists and has size greater than zero.

-tfd

true if file descriptor number fd
is open and associated with a terminal device.
(note: fd is not optional)

-ufile

true if file exists and has its setuid bit set.

-wfile

true if file exists and is writable by current process.

-xfile

true if file exists and is executable by current process.
If file exists and is a directory, then the current process
has permission to search in the directory.

-zstring

true if length of string is zero.

-Lfile

true if file exists and is a symbolic link.

-Ofile

true if file exists and is owned by the effective user ID of this process.

-Gfile

true if file exists and its group matches
the effective group ID of this process.

-Sfile

true if file exists and is a socket.

-Nfile

true if file exists and its access time is
not newer than its modification time.

file1-ntfile2

true if file1 exists and is newer than file2.

file1-otfile2

true if file1 exists and is older than file2.

file1-effile2

true if file1 and file2 exist and refer to the same file.

string=pattern

string==pattern

true if string matches pattern.
The `==' form is the preferred one. The `=' form is for
backward compatibility and should be considered obsolete.

string!=pattern

true if string does not match pattern.

string1<string2

true if string1 comes before string2
based on ASCII value of their characters.

string1>string2

true if string1 comes after string2
based on ASCII value of their characters.

exp1-eqexp2

true if exp1 is numerically equal to exp2.

exp1-neexp2

true if exp1 is numerically not equal to exp2.

exp1-ltexp2

true if exp1 is numerically less than exp2.

exp1-gtexp2

true if exp1 is numerically greater than exp2.

exp1-leexp2

true if exp1 is numerically less than or equal to exp2.

exp1-geexp2

true if exp1 is numerically greater than or equal to exp2.

(exp)

true if exp is true.

!exp

true if exp is false.

exp1&&exp2

true if exp1 and exp2 are both true.

exp1||exp2

true if either exp1 or exp2 is true.

Normal shell expansion is performed on the file, string and
pattern arguments, but the result of each expansion is constrained to
be a single word, similar to the effect of double quotes. However, pattern
metacharacters are active for the pattern arguments; the patterns
are the same as those used for filename generation, see
13.8 Filename Generation, but there is no special behaviour
of `/' nor initial dots, and no glob qualifiers are allowed.

In each of the above expressions, if
file is of the form `/dev/fd/n',
where n is an integer,
then the test applied to the open file whose
descriptor number is n,
even if the underlying system does not support
the /dev/fd directory.

In the forms which do numeric comparison, the expressions exp
undergo arithmetic expansion as if they were enclosed in $((...)).

For example, the following:

[[ ( -f foo || -f bar ) && $report = y* ]] && print File exists.

tests if either file foo or file bar exists, and if so, if the
value of the parameter report begins with `y'; if the complete
condition is true, the message `File exists.' is printed.