Table of Contents generated with DocToc
12 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:
-
-a
file
true iffile
exists. -
-b
file
true iffile
exists and is a block special file. -
-c
file
true iffile
exists and is a character special file. -
-d
file
true iffile
exists and is a directory. -
-e
file
true iffile
exists. -
-f
file
true iffile
exists and is a regular file. -
-g
file
true iffile
exists and has its setgid bit set. -
-h
file
true iffile
exists and is a symbolic link. -
-k
file
true iffile
exists and has its sticky bit set. -
-n
string
true if length ofstring
is non-zero. -
-o
option
true if option namedoption
is on.option
may be a single character, in which case it is a single letter option name. (See Specifying Options.)When no option named
option
exists, and thePOSIX_BUILTINS
option hasn’t been set, return 3 with a warning. If that option is set, return 1 with no warning. -
-p
file
true iffile
exists and is a FIFO special file (named pipe). -
-r
file
true iffile
exists and is readable by current process. -
-s
file
true iffile
exists and has size greater than zero. -
-t
fd
true if file descriptor numberfd
is open and associated with a terminal device. (note:fd
is not optional) -
-u
file
true iffile
exists and has its setuid bit set. -
-v
varname
true if shell variablevarname
is set. -
-w
file
-x
file
-z
string
true if length ofstring
is zero. -
-L
file
true iffile
exists and is a symbolic link. -
-O
file
true iffile
exists and is owned by the effective user ID of this process. -
-G
file
true iffile
exists and its group matches the effective group ID of this process. -
-S
file
true iffile
exists and is a socket. -
-N
file
true iffile
exists and its access time is not newer than its modification time. -
file1
-nt
file2
true iffile1
exists and is newer thanfile2
. -
file1
-ot
file2
true iffile1
exists and is older thanfile2
. -
file1
-ef
file2
true iffile1
andfile2
exist and refer to the same file. -
string
=
pattern
string
==
pattern
true ifstring
matchespattern
. The two forms are exactly equivalent. The ‘=
’ form is the traditional shell syntax (and hence the only one generally used with thetest
and[
builtins); the ‘==
’ form provides compatibility with other sorts of computer language. -
string
!=
pattern
true ifstring
does not matchpattern
. -
string
=~
regexp
true ifstring
matches the regular expressionregexp
. If the optionRE_MATCH_PCRE
is setregexp
is tested as a PCRE regular expression using thezsh/pcre
module, else it is tested as a POSIX extended regular expression using thezsh/regex
module. Upon successful match, some variables will be updated; no variables are changed if the matching fails.If the option
BASH_REMATCH
is not set the scalar parameterMATCH
is set to the substring that matched the pattern and the integer parametersMBEGIN
andMEND
to the index of the start and end, respectively, of the match instring
, such that ifstring
is contained in variablevar
the expression ‘${var[$MBEGIN,$MEND]}
’ is identical to ‘$MATCH
’. The setting of the optionKSH_ARRAYS
is respected. Likewise, the arraymatch
is set to the substrings that matched parenthesised subexpressions and the arraysmbegin
andmend
to the indices of the start and end positions, respectively, of the substrings withinstring
. The arrays are not set if there were no parenthesised subexpressions. For example, if the string ‘a short string
’ is matched against the regular expression ‘s(...)t
’, then (assuming the optionKSH_ARRAYS
is not set)MATCH
,MBEGIN
andMEND
are ‘short
’,3
and7
, respectively, whilematch
,mbegin
andmend
are single entry arrays containing the strings ‘hor
’, ‘4
’ and ‘6
’, respectively.If the option
BASH_REMATCH
is set the arrayBASH_REMATCH
is set to the substring that matched the pattern followed by the substrings that matched parenthesised subexpressions within the pattern. -
string1
<
string2
true ifstring1
comes beforestring2
based on ASCII value of their characters. -
string1
>
string2
true ifstring1
comes afterstring2
based on ASCII value of their characters. -
exp1
-eq
exp2
true ifexp1
is numerically equal toexp2
. Note that for purely numeric comparisons use of the((``...``))
builtin described in Arithmetic Evaluation is more convenient than conditional expressions. -
exp1
-ne
exp2
true ifexp1
is numerically not equal toexp2
. -
exp1
-lt
exp2
true ifexp1
is numerically less thanexp2
. -
exp1
-gt
exp2
true ifexp1
is numerically greater thanexp2
. -
exp1
-le
exp2
true ifexp1
is numerically less than or equal toexp2
. -
exp1
-ge
exp2
true ifexp1
is numerically greater than or equal toexp2
. -
(
exp
)
true ifexp
is true. -
!
exp
true ifexp
is false. -
exp1
&&
exp2
true ifexp1
andexp2
are both true. -
exp1
||
exp2
true if eitherexp1
orexp2
is true.
For compatibility, if there is a single argument that is not
syntactically significant, typically a variable, the condition is
treated as a test for whether the expression expands as a string of
non-zero length. In other words, [[ $var ]]
is the same as [[ -n $var ]]
. It is recommended that the second, explicit, form be used where
possible.
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.
Filename generation is not performed on any form of argument to
conditions. However, it can be forced in any case where normal shell
expansion is valid and when the option EXTENDED_GLOB
is in effect by
using an explicit glob qualifier of the form (#q)
at the end of the
string. A normal glob qualifier expression may appear between the ‘q
’
and the closing parenthesis; if none appears the expression has no
effect beyond causing filename generation. The results of filename
generation are joined together to form a single word, as with the
results of other forms of expansion.
This special use of filename generation is only available with the [[
syntax. If the condition occurs within the [
or test
builtin
commands then globbing occurs instead as part of normal command line
expansion before the condition is evaluated. In this case it may
generate multiple words which are likely to confuse the syntax of the
test command.
For example,
[[ -n file*(#qN) ]]
produces status zero if and only if there is at least one file in the
current directory beginning with the string ‘file
’. The globbing
qualifier N
ensures that the expression is empty if there is no
matching file.
Pattern metacharacters are active for the pattern
arguments; the
patterns are the same as those used for filename generation, see
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.
This document was generated on February 15, 2020 using
texi2html 5.0.
Zsh version 5.8, released on February 14, 2020.