<HTML> <HEAD> <!-- Created by texi2html 1.56k from ./zsh.texi on 26 June 2001 --> <TITLE>zsh - Conditional Expressions</TITLE> </HEAD> <BODY> Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_10.html">previous</A>, <A HREF="zsh_12.html">next</A>, <A HREF="zsh_29.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>. <P><HR><P> <H1><A NAME="SEC38" HREF="zsh_toc.html#TOC38">Conditional Expressions</A></H1> <P> <A NAME="IDX128"></A> <A NAME="IDX129"></A> A <EM>conditional expression</EM> is used with the <TT>[[</TT> 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: <P> <DL COMPACT> <DT><TT>-a</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists. <DT><TT>-b</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a block special file. <DT><TT>-c</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a character special file. <DT><TT>-d</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a directory. <DT><TT>-e</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists. <DT><TT>-f</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a regular file. <DT><TT>-g</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and has its setgid bit set. <DT><TT>-h</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a symbolic link. <DT><TT>-k</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and has its sticky bit set. <DT><TT>-n</TT> <VAR>string</VAR> <DD> true if length of <VAR>string</VAR> is non-zero. <DT><TT>-o</TT> <VAR>option</VAR> <DD> true if option named <VAR>option</VAR> is on. <VAR>option</VAR> may be a single character, in which case it is a single letter option name. (See section <A HREF="zsh_15.html#SEC76">Specifying Options</A>.) <DT><TT>-p</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a FIFO special file (named pipe). <DT><TT>-r</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is readable by current process. <DT><TT>-s</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and has size greater than zero. <DT><TT>-t</TT> <VAR>fd</VAR> <DD> true if file descriptor number <VAR>fd</VAR> is open and associated with a terminal device. (note: <VAR>fd</VAR> is not optional) <DT><TT>-u</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and has its setuid bit set. <DT><TT>-w</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is writable by current process. <DT><TT>-x</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is executable by current process. If <VAR>file</VAR> exists and is a directory, then the current process has permission to search in the directory. <DT><TT>-z</TT> <VAR>string</VAR> <DD> true if length of <VAR>string</VAR> is zero. <DT><TT>-L</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a symbolic link. <DT><TT>-O</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is owned by the effective user ID of this process. <DT><TT>-G</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and its group matches the effective group ID of this process. <DT><TT>-S</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and is a socket. <DT><TT>-N</TT> <VAR>file</VAR> <DD> true if <VAR>file</VAR> exists and its access time is not newer than its modification time. <DT><VAR>file1</VAR> <TT>-nt</TT> <VAR>file2</VAR> <DD> true if <VAR>file1</VAR> exists and is newer than <VAR>file2</VAR>. <DT><VAR>file1</VAR> <TT>-ot</TT> <VAR>file2</VAR> <DD> true if <VAR>file1</VAR> exists and is older than <VAR>file2</VAR>. <DT><VAR>file1</VAR> <TT>-ef</TT> <VAR>file2</VAR> <DD> true if <VAR>file1</VAR> and <VAR>file2</VAR> exist and refer to the same file. <DT><VAR>string</VAR> <TT>=</TT> <VAR>pattern</VAR> <DD> <DT><VAR>string</VAR> <TT>==</TT> <VAR>pattern</VAR> <DD> true if <VAR>string</VAR> matches <VAR>pattern</VAR>. The `<TT>==</TT>' form is the preferred one. The `<TT>=</TT>' form is for backward compatibility and should be considered obsolete. <DT><VAR>string</VAR> <TT>!=</TT> <VAR>pattern</VAR> <DD> true if <VAR>string</VAR> does not match <VAR>pattern</VAR>. <DT><VAR>string1</VAR> <TT><</TT> <VAR>string2</VAR> <DD> true if <VAR>string1</VAR> comes before <VAR>string2</VAR> based on ASCII value of their characters. <DT><VAR>string1</VAR> <TT>></TT> <VAR>string2</VAR> <DD> true if <VAR>string1</VAR> comes after <VAR>string2</VAR> based on ASCII value of their characters. <DT><VAR>exp1</VAR> <TT>-eq</TT> <VAR>exp2</VAR> <DD> true if <VAR>exp1</VAR> is numerically equal to <VAR>exp2</VAR>. <DT><VAR>exp1</VAR> <TT>-ne</TT> <VAR>exp2</VAR> <DD> true if <VAR>exp1</VAR> is numerically not equal to <VAR>exp2</VAR>. <DT><VAR>exp1</VAR> <TT>-lt</TT> <VAR>exp2</VAR> <DD> true if <VAR>exp1</VAR> is numerically less than <VAR>exp2</VAR>. <DT><VAR>exp1</VAR> <TT>-gt</TT> <VAR>exp2</VAR> <DD> true if <VAR>exp1</VAR> is numerically greater than <VAR>exp2</VAR>. <DT><VAR>exp1</VAR> <TT>-le</TT> <VAR>exp2</VAR> <DD> true if <VAR>exp1</VAR> is numerically less than or equal to <VAR>exp2</VAR>. <DT><VAR>exp1</VAR> <TT>-ge</TT> <VAR>exp2</VAR> <DD> true if <VAR>exp1</VAR> is numerically greater than or equal to <VAR>exp2</VAR>. <DT><TT>(</TT> <VAR>exp</VAR> <TT>)</TT> <DD> true if <VAR>exp</VAR> is true. <DT><TT>!</TT> <VAR>exp</VAR> <DD> true if <VAR>exp</VAR> is false. <DT><VAR>exp1</VAR> <TT>&&</TT> <VAR>exp2</VAR> <DD> true if <VAR>exp1</VAR> and <VAR>exp2</VAR> are both true. <DT><VAR>exp1</VAR> <TT>||</TT> <VAR>exp2</VAR> <DD> true if either <VAR>exp1</VAR> or <VAR>exp2</VAR> is true. </DL> <P> Normal shell expansion is performed on the <VAR>file</VAR>, <VAR>string</VAR> and <VAR>pattern</VAR> 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 <VAR>pattern</VAR> arguments; the patterns are the same as those used for filename generation, see section <A HREF="zsh_13.html#SEC56">Filename Generation</A>, but there is no special behaviour of `<TT>/</TT>' nor initial dots, and no glob qualifiers are allowed. <P> In each of the above expressions, if <VAR>file</VAR> is of the form `<TT>/dev/fd/</TT><VAR>n</VAR>', where <VAR>n</VAR> is an integer, then the test applied to the open file whose descriptor number is <VAR>n</VAR>, even if the underlying system does not support the <TT>/dev/fd</TT> directory. <P> In the forms which do numeric comparison, the expressions <VAR>exp</VAR> undergo arithmetic expansion as if they were enclosed in <TT>$((...))</TT>. <P> For example, the following: <P> <PRE> [[ ( -f foo || -f bar ) && $report = y* ]] && print File exists. </PRE> <P> tests if either file <TT>foo</TT> or file <TT>bar</TT> exists, and if so, if the value of the parameter <TT>report</TT> begins with `<TT>y</TT>'; if the complete condition is true, the message `<TT>File exists.</TT>' is printed. <P><HR><P> Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_10.html">previous</A>, <A HREF="zsh_12.html">next</A>, <A HREF="zsh_29.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>. </BODY> </HTML>