C shell expressions and operators

The @ built-in command and the exit, if, and while statements accept expressions that include operators similar to those of C language, with the same precedence.

The following operators are available:

Operator What it means
() change precedence
~ complement
! negation
*/ % multiply, divide, modulo
+ - add, subtract
<< > > left shift, right shift
<= >= < > relational operators
== != =~ !~ string comparison/pattern matching
& bitwise AND
^ bitwise exclusive OR
| bitwise inclusive OR
&& logical AND
|| logical OR
In the previous list, precedence of the operators decreases down the list (left to right, top to bottom).
Note: The operators + and - are right-associative. For example, evaluation of a + b - c is performed as follows:
a + (b - c)
and not as follows:
(a + b) - c

The ==, !=, =~, and !~ operators compare their arguments as strings; all others operate on numbers. The =~ and !~ operators are similar to == and != , except that the rightmost side is a pattern against which the leftmost operand is matched. This reduces the need for use of the switch statement in shell procedures.

The logical operators or (||) and and (&&) are also available. They can be used to check for a range of numbers, as in the following example:
if ($#argv > 2 && $#argv < 7) then
In the preceding example, the number of arguments must be greater than 2 and less than 7.

Strings beginning with zero (0) are considered octal numbers. Null or missing arguments are considered 0. All expressions result in strings representing decimal numbers. Note that two components of an expression can appear in the same word. Except when next to components of expressions that are syntactically significant to the parser (& | < > ( )), expression components should be surrounded by spaces.

Also available in expressions as primitive operands are command executions enclosed in parentheses ( ) and file inquiries of the form (-operator Filename), where operator is one of the following:

Item Description
r Read access
w Write access
x Execute access
e Existence
o Ownership
z Zero size
f Plain file
d Directory

The specified Filename is command and file name expanded and then tested to see if it has the specified relationship to the real user. If Filename does not exist or is inaccessible, all inquiries return false(0). If the command runs successfully, the inquiry returns a value of true(1). Otherwise, if the command fails, the inquiry returns a value of false(0). If more detailed status information is required, run the command outside an expression and then examine the status shell variable.