Classic Shell Scripting - Arnold Robbins [207]
Table 14-3. Extended test operators
Operator
bash/ksh only
True if ...
-a file
file exists. (Obsolete. -e is preferred.)
-b file
file is a block device file.
-c file
file is a character device file.
-C file
ksh
file is a contiguous file. (Not for most Unix versions.)
-d file
file is a directory.
-e file
file exists.
-f file
file is a regular file.
-g file
file has its setgid bit set.
-G file
file's group ID is the same as the effective group ID of the shell.
-h file
file is a symbolic link.
-k file
file has its sticky bit set.
-l file
ksh
file is a symbolic link. (Works only on systems where /bin/test -l tests for symbolic links.)
-L file
file is a symbolic link.
-n string
string is non-null.
-N file
bash
file was modified since it was last read.
-o option
option is set.
-O file
file is owned by the shell's effective user ID.
-p file
file is a pipe or named pipe (FIFO file).
-r file
file is readable.
-s file
file is not empty.
-S file
file is a socket.
-t n
File descriptor n points to a terminal.
-u file
file has its setuid bit set.
-w file
file is writable.
-x file
file is executable, or is a directory that can be searched.
-z string
string is null.
fileA -nt fileB
fileA is newer than fileB, or fileB does not exist.
fileA -ot fileB
fileA is older than fileB, or fileB does not exist.
fileA -ef fileB
fileA and fileB point to the same file.
string = pattern
ksh
string matches pattern (which can contain wildcards). Obsolete; = = is preferred.
string = = pattern
string matches pattern (which can contain wildcards).
string != pattern
string does not match pattern.
stringA < stringB
stringA comes before stringB in dictionary order.
stringA > stringB
stringA comes after stringB in dictionary order.
exprA -eq exprB
Arithmetic expressions exprA and exprB are equal.
exprA -ne exprB
Arithmetic expressions exprA and exprB are not equal.
exprA -lt exprB
exprA is less than exprB.
exprA -gt exprB
exprA is greater than exprB.
exprA -le exprB
exprA is less than or equal to exprB.
exprA -ge exprB
exprA is greater than or equal to exprB.
The operators can be logically combined with && (AND) and || (OR) and grouped with parentheses. They may also be negated with !. When used with filenames of the form /dev/fd/ n, they test the corresponding attribute of open file descriptor n.
The operators -eq, -ne, -lt, -le, -gt, and -ge are considered obsolete in ksh93; the let command or ((...)) should be used instead. (The let command and ((...)) are described briefly in Section 14.3.7.)
Extended Pattern Matching
ksh88 introduced additional pattern-matching facilities that give the shell power roughly equivalent to awk and egrep extended regular expressions. (Regular expressions are described in detail in Section 3.2.) With the extglob option enabled, bash also supports these operators. (They're always enabled in ksh.) Table 14-4 summarizes the additional facilities.
Table 14-4. Shell versus egrep/awk regular expression operators
ksh/bash
egrep/awk
Meaning
*( exp )
exp *
0 or more occurrences of exp
+( exp )
exp +
1 or more occurrences of exp
?( exp )
exp ?
0 or 1 occurrences of exp
@( exp1 | exp2 |...)
exp1 | exp2 |...
exp1 or exp2 or ...
!( exp )
(none)
Anything that doesn't match exp
The notations for shell regular expressions and standard regular expressions are very similar, but they're not identical. Because the shell would interpret an expression like dave|fred|bob as a pipeline of commands, you must use @(dave|fred|bob) for alternates by themselves.
For example:
@(dave|fred|bob) matches dave, fred, or bob.
*(dave|fred|bob) means 0 or