.. historic nodename: Conditional Expressions

Conditional Expressions
================================================================================

.. only:: man

    Conditional Expressions
    --------------------------------------------------------------------------------
    

.. index:: single: conditional expressions; Command

.. index:: single: expressions, conditional; Command

A :emphasis:`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`` :var:`file`
    
    true if :var:`file` exists.
    

``-b`` :var:`file`
    
    true if :var:`file` exists and is a block special file.
    

``-c`` :var:`file`
    
    true if :var:`file` exists and is a character special file.
    

``-d`` :var:`file`
    
    true if :var:`file` exists and is a directory.
    

``-e`` :var:`file`
    
    true if :var:`file` exists.
    

``-f`` :var:`file`
    
    true if :var:`file` exists and is a regular file.
    

``-g`` :var:`file`
    
    true if :var:`file` exists and has its setgid bit set.
    

``-h`` :var:`file`
    
    true if :var:`file` exists and is a symbolic link.
    

``-k`` :var:`file`
    
    true if :var:`file` exists and has its sticky bit set.
    

``-n`` :var:`string`
    
    true if length of :var:`string` is non-zero.
    

``-o`` :var:`option`
    
    true if option named :var:`option` is on.  :var:`option`
    may be a single character, in which case it is a single letter option name.
    (See :zshsectref:`Specifying Options <zshoptions>`.)
    
    When no option named :var:`option` exists, and the ``POSIX_BUILTINS`` option
    hasn't been set, return 3 with a warning.  If that option is set, return 1
    with no warning.
    

``-p`` :var:`file`
    
    true if :var:`file` exists and is a FIFO special file (named pipe).
    

``-r`` :var:`file`
    
    true if :var:`file` exists and is readable by current process.
    

``-s`` :var:`file`
    
    true if :var:`file` exists and has size greater than zero.
    

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

``-u`` :var:`file`
    
    true if :var:`file` exists and has its setuid bit set.
    

``-v`` :var:`varname`
    
    true if shell variable :var:`varname` is set.
    

``-w`` :var:`file`
    
    true if :var:`file` exists and is writable by current process.
    

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

``-z`` :var:`string`
    
    true if length of :var:`string` is zero.
    

``-L`` :var:`file`
    
    true if :var:`file` exists and is a symbolic link.
    

``-O`` :var:`file`
    
    true if :var:`file` exists and is owned by the effective user ID of this process.
    

``-G`` :var:`file`
    
    true if :var:`file` exists and its group matches
    the effective group ID of this process.
    

``-S`` :var:`file`
    
    true if :var:`file` exists and is a socket.
    

``-N`` :var:`file`
    
    true if :var:`file` exists and its access time is
    not newer than its modification time.
    

:var:`file1` ``-nt`` :var:`file2`
    
    true if :var:`file1` exists and is newer than :var:`file2`.
    

:var:`file1` ``-ot`` :var:`file2`
    
    true if :var:`file1` exists and is older than :var:`file2`.
    

:var:`file1` ``-ef`` :var:`file2`
    
    true if :var:`file1` and :var:`file2` exist and refer to the same file.
    

:var:`string` ``=`` :var:`pattern`

:var:`string` ``==`` :var:`pattern`
    
    true if :var:`string` matches :var:`pattern`.
    The two forms are exactly equivalent.  The ``=`` form is
    the traditional shell syntax (and hence the only one generally used
    with the ``test`` and ``[`` builtins); the ``==`` form provides
    compatibility with other sorts of computer language.
    

:var:`string` ``!=`` :var:`pattern`
    
    true if :var:`string` does not match :var:`pattern`.
    

:var:`string` ``=~`` :var:`regexp`
    
    true if :var:`string` matches the regular expression
    :var:`regexp`.  If the option ``RE_MATCH_PCRE`` is set
    :var:`regexp` is tested as a PCRE regular expression using
    the ``zsh/pcre`` module, else it is tested as a POSIX
    extended regular expression using the ``zsh/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 parameter
    ``MATCH`` is set to the substring that matched the pattern and
    the integer parameters ``MBEGIN`` and ``MEND`` to the index of the start
    and end, respectively, of the match in :var:`string`, such that if
    :var:`string` is contained in variable ``var`` the expression
    ``${var[$MBEGIN,$MEND]}`` is identical to ``$MATCH``.  The setting
    of the option ``KSH_ARRAYS`` is respected.  Likewise, the array
    ``match`` is set to the substrings that matched parenthesised
    subexpressions and the arrays ``mbegin`` and ``mend`` to the indices of
    the start and end positions, respectively, of the substrings within
    :var:`string`.  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
    option ``KSH_ARRAYS`` is not set) ``MATCH``, ``MBEGIN``
    and ``MEND`` are ``short``, ``3`` and ``7``, respectively, while ``match``,
    ``mbegin`` and ``mend`` are single entry arrays containing
    the strings ``hor``, ``4`` and ``6``, respectively.
    
    If the option ``BASH_REMATCH`` is set the array
    ``BASH_REMATCH`` is set to the substring that matched the pattern
    followed by the substrings that matched parenthesised
    subexpressions within the pattern.
    

:var:`string1` ``<`` :var:`string2`
    
    true if :var:`string1` comes before :var:`string2`
    based on ASCII value of their characters.
    

:var:`string1` ``>`` :var:`string2`
    
    true if :var:`string1` comes after :var:`string2`
    based on ASCII value of their characters.
    

:var:`exp1` ``-eq`` :var:`exp2`
    
    true if :var:`exp1` is numerically equal to :var:`exp2`.
    Note that for purely numeric comparisons use of the
    ``((``:var:`...`\ ``))`` builtin described in
    :zshsectref:`Arithmetic Evaluation <above>` is more convenient than
    conditional expressions.
    

:var:`exp1` ``-ne`` :var:`exp2`
    
    true if :var:`exp1` is numerically not equal to :var:`exp2`.
    

:var:`exp1` ``-lt`` :var:`exp2`
    
    true if :var:`exp1` is numerically less than :var:`exp2`.
    

:var:`exp1` ``-gt`` :var:`exp2`
    
    true if :var:`exp1` is numerically greater than :var:`exp2`.
    

:var:`exp1` ``-le`` :var:`exp2`
    
    true if :var:`exp1` is numerically less than or equal to :var:`exp2`.
    

:var:`exp1` ``-ge`` :var:`exp2`
    
    true if :var:`exp1` is numerically greater than or equal to :var:`exp2`.
    

``(`` :var:`exp` ``)``
    
    true if :var:`exp` is true.
    

``!`` :var:`exp`
    
    true if :var:`exp` is false.
    

:var:`exp1` ``&&`` :var:`exp2`
    
    true if :var:`exp1` and :var:`exp2` are both true.
    

:var:`exp1` ``||`` :var:`exp2`
    
    true if either :var:`exp1` or :var:`exp2` 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 :var:`file`, :var:`string` and
:var:`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,

.. code-block:: zsh
   :class: 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 :var:`pattern` arguments;
the patterns are the same as those used for filename generation, see
:zshsectref:`Filename Generation <zshexpn>`, but there is no special behaviour
of ``/`` nor initial dot, and the patterns ``**/`` and ``***/`` behave
the same as ``*/``, in which the ``*`` has its standard behaviour
but may also match further ``/`` characters.  Also, no bare glob
qualifiers are allowed, though the form ((#q):var:`...`) is allowed as
shown above.

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

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

For example, the following:

.. code-block:: zsh
   :class: example

    [[ ( -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.
