Expressions

Expressions are used in @, if, and while statements to perform arithmetic, string comparisons, file testing, and so on. exit and set also specify expressions, as can the tcsh built-in command filetest. Expressions are formed by combining variables and constants with operators that resemble those in the C programming language. Operator precedence is the same as in C but can be remembered as follows:

  1. * / %

  2. + -

Group all other expressions inside parentheses. Parentheses are required if the expression contains <, >, &, or |.

Operators

Operators can be one of the following types:

Assignment operators

OperatorDescription
= Assign value.
+= -= Reassign after addition/subtraction.
*= /= %= Reassign after multiplication/division/remainder.
&= ^= |= Reassign after bitwise AND/XOR/OR.
++ Increment.
-- Decrement.

Arithmetic operators

OperatorDescription
* / % Multiplication; integer division; modulus (remainder)
+ - Addition; subtraction

Bitwise and logical operators

OperatorDescription
~ Binary inversion (one’s complement).
! Logical negation.
<< >> Bitwise left shift; bitwise right shift.
& Bitwise AND.
^ Bitwise exclusive OR.
| Bitwise OR.
&& Logical AND.
|| Logical OR.
{ command }

Return 1 if command is successful, 0 otherwise. Note that this is the opposite of command’s normal return code. The $status variable may be more practical.

Comparison operators

OperatorDescription
== != Equality; inequality
<= >= Less than or equal to; greater than or equal to
< > Less than; greater than ...

Get Linux in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.