Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Conditional expressions

&pagelevel(4)&pagelevel

A conditional expression can be used to test attributes of files and to compare algebraic expressions and strings. In the POSIX shell, conditional expressions are specified as part of a compound command of the form [[...]]. Blank interpretation and file name generation are not performed on the words of the conditional expression between [[ and ]]. Each conditional expression can be constructed from one or more of the following unary or binary expressions:

In each of the expressions below, if file is of the form /dev/fd/n, where fd is the file descriptor and n is an integer, then the test is applied to the open file whose descriptor number is n.

-a file

(access) True if file exists.

-b file

(block device) True if file exists and is a block special file.

-c file

(character device) True if file exists and is a character special file.

-d file

(directory) True if file exists and is a directory.

-f file

(file) True if file exists and is an ordinary file.

-g file

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

-k file

(sticky) True if file exists and is has its sticky bit set.

-o option

(option) True if the named option is turned on (option can be set with set). You must use the full option name, e.g. errexit.

-p file

(pipe) True if file exists and is a FIFO special file or a pipe.

-r file

(read) True if file exists and the current process has read permission for it.

-s file

(size) True if file exists and has a size greater than zero.

-t filedes

(terminal) True if file descriptor number fildes is open and is associated with a terminal.

-u file

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

-w file

(write) True if file exists and the current process has write permission for it.

-x file

(execute) True if file exists and the current process has execute permission for it. If file exists and is a directory, then the current process must have permission to search in the directory.

-G file

(group) True if file exists and its group matches the effective group ID of the current process.

-L file

(symbolic link) True if file exists and is a symbolic link.

-O file

(owner) True if file exists and is owned by the effective user ID of the current process.

-S file

(socket) True if file exists and is a socket.

file1 -nt file2

(newer than) True if file1 exists and is newer than file2.

file1 -ot file2

(older than) True if file1 exists and is older than file2.

file1 -ef file2

(equal file) True if file1 and file2 exist and are links to the same file.

String attributes and comparisons

-n string

(non-zero) True if string exists and is not a null string, i.e. if the length of the string is greater than 0.

-z string

(zero) True if the specified string is a null string, i.e. if the length of the string is 0.

string = pattern

True if string matches pattern.

string != pattern

True if string does not match pattern.

string1 < string2

True if string1 comes before string2 in the EBCDIC collating sequence.

string1 > string2

True if string1 comes after string2 based in the EBCDIC collating sequence.

Algebraic comparisons between integers

expr1 -eq expr2

(equal) True if expr1 is equal to expr2.

expr1 -ne expr2

(not equal) True if expr1 is not equal to expr2.

expr1 -lt expr2

(less than) True if expr1 is less than expr2.

expr1 -gt expr2

(greater than) True if expr1 is greater than expr2.

expr1 -le expr2

(less than or equal) True if expr1 is less than or equal to expr2.

expr1 -ge expr2

(greater than or equal) True if expr1 is greater than or equal to expr2.

Negated and compound expressions

A compound expression can be constructed from the above expressions with any of the following mechanisms. The mechanisms are listed in decreasing order of precedence:

( expression )

True if expression is true. The enclosed expression can be a single expression or a group of concatenated expressions.

! expression

Negation: true if expression is false.

expression1 && expression2

Logical AND: True if expression1 and expression2 are both true.

expression1 || expression2

Logical OR: True if either expression1 or expression2 is true.