Logical, arithmetical and comparison operators

These operators are similar to those of C and have the same precedence.
They include

  ||  &&  |  ^   &  ==  !=  =~  !~  <=  >=  <  > <<  >>  +  -  *   /  %  !  ~  (  )

       Here the precedence increases to the right, == !=  =~  and !~,
       <=  >=  < and  >,  << and >>, + and -, * / and %
       being, in groups, at the same level.  The == != =~ and !~ operators
       compare  their  arguments as strings; all others operate on numbers.
       The operators '=~' and '!~' are like '!=' and '=='  except  that
       the  right hand side is a glob-pattern against which the left hand
       operand is matched. This reduces the need for use of the switch builtin
       command in shell scripts when all that is really needed is pattern

       Strings which begin with '0' are considered  octal  numbers.   Null  or
       missing  arguments  are considered '0'.  The results of all expressions
       are strings, which represent decimal numbers.  It is important to  note
       that  no   two  components of an expression can appear in the same word;
       except when adjacent to components of expressions which  are  syntacti-
       cally  significant  to the parser ('&' '|' '<' '>' '(' ')') they should
       be surrounded by spaces.

   Command exit status

       Commands can be executed in expressions and their exit status  returned
       by enclosing them in braces ('{}').  Remember that the braces should be
       separated from the words of the command by spaces.  Command  executions
       succeed, returning true, i.e., '1', if the command exits with status 0,
       otherwise they fail, returning false, i.e., '0'.   If more detailed sta-
       tus information is required then the command should be executed outside
       of an expression and the status shell variable examined.

   File inquiry operators
       Some of these operators perform true/false tests on files  and  related
       objects.   They are of the form -op file, where op is one of

     r   Read access
     w   Write access
     x   Execute access
     X   Executable  in the path or shell builtin, e.g., '-X ls' and '-X
         ls-F' are generally true, but '-X /bin/ls' is not (+)
     e   Existence
     o   Ownership
     z   Zero size
     s   Non-zero size (+)
     f   Plain file
     d   Directory
     l   Symbolic link (+) *
     b   Block special file (+)
     c   Character special file (+)
     p   Named pipe (fifo) (+) *
     S   Socket special file (+) *
     u   Set-user-ID bit is set (+)
     g   Set-group-ID bit is set (+)
     k   Sticky bit is set (+)
     t   file (which must be a digit) is an open file descriptor  for  a
         terminal device (+)
     R   Has been migrated (convex only) (+)
     L   Applies  subsequent  operators in a multiple-operator test to a
         symbolic link rather than to the file to which the link  points
         (+) *

       file  is command and filename expanded and then tested to see if it has
       the specified relationship to the real user.  If file does not exist or
       is  inaccessible   or, for the operators indicated by '*', if the speci-
       fied file type does not exist on the current system, then all enquiries
       return false, i.e., '0'.

       These  operators may be combined for conciseness: '-xy file' is equiva-
       lent to '-x file && -y file'.  (+) For example, '-fx' is true  (returns
       '1') for plain executable files, but not for directories.

       L may be used in a multiple-operator test to apply subsequent operators
       to a symbolic link rather than to the file to which  the   link  points.
       For  example, '-lLo' is true for links owned by the invoking user.  Lr,
       Lw and Lx are always true for links and false for non-links.  L  has  a
       different  meaning  when it is the last operator in a multiple-operator
       test; see below.

       It is possible but not useful, and  sometimes  misleading,  to  combine
       operators  which   expect file to be a file with operators which do not,
       (e.g., X and t).   Following L with a non-file operator can lead to par-
       ticularly strange results.

       Other  operators   return   other information, i.e., not just '0' or '1'.
       (+) They have the same format as before; op may be one of

     A     Last file access time, as the number of seconds  since  the epoch
     A:    Like A, but in timestamp format, e.g., 'Fri May 14 16:36:10  1993'
     M     Last file modification time
     M:    Like M, but in timestamp format
     C     Last inode modification time
     C:    Like C, but in timestamp format
     D     Device number
     I     Inode number
     F     Composite file identifier, in the form device:inode
     L     The name of the file pointed to by a symbolic link
     N     Number of (hard) links
     P     Permissions, in octal, without leading zero
     P:    Like P, with leading zero
     Pmode   Equivalent to '-P file & mode', e.g., '-P22  file'  returns '22'
             if  file is writable by group and other, '20' if by
              group only, and '0' if by neither
     Pmode:  Like Pmode:, with leading zero
     U     Numeric userid
     U:    Username, or the numeric userid if the username is unknown
     G     Numeric groupid
     G:    Groupname, or the  numeric  groupid  if  the   groupname  is unknown
     Z     Size, in bytes

Only one of these operators may appear in a multiple-operator test, and it must be the last. Note that L has a different meaning at the end of and elsewhere in a multiple-operator test. Because '0' is a valid return value for many of these operators, they do not return '0' when they fail: most return '-1', and F returns ':'.

If the shell is compiled with POSIX defined (see the version shell variable), the result of a file inquiry is based on the permission bits of the file and not on the result of the access(2) system call. For example, if one tests a file with -w whose permissions would ordinarily allow writing but which is on a file system mounted read-only, the test will succeed in a POSIX shell but fail in a non-POSIX shell.

File inquiry operators can also be evaluated with the filetest builtin command

Related commands:

OS X Syntax

© Copyright SS64.com 1999-2016
Some rights reserved