UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: 5.3 VariablesChapter 5
The C Shell
Next: 5.5 Command History csh shell

5.4 Expressions

Expressions are used in @, if, and while statements to perform arithmetic, string comparisons, file testing, etc. exit and set can also specify expressions. 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 ( )'s. Parentheses are required if the expression contains <, >, &, or |.

5.4.1 Operators

Operators can be one of the following types: Assignment Operators

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

* / %Multiplication; integer division; modulus (remainder).
+ -Addition; subtraction. Bitwise and Logical Operators

~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

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

String on left matches a filename pattern containing *, ?, or [...].


String on left does not match a filename pattern containing *, ?, or [...]. File Inquiry Operators

Command substitution and filename expansion are performed on file before the test is performed.

-d fileThe file is a directory.
-e fileThe file exists.
-f fileThe file is a plain file.
-o fileThe user owns the file.
-r fileThe user has read permission.
-w fileThe user has write permission.
-x fileThe user has execute permission.
-z fileThe file has zero size.
!Reverse the sense of any inquiry above.

5.4.2 Examples

The following examples show @ commands and assume n = 4:

ExpressionValue of $x
@ x = ($n > 10 || $n < 5)1
@ x = ($n >= 0 && $n < 3)0
@ x = ($n << 2)16
@ x = ($n >> 2)1
@ x = $n % 20
@ x = $n % 31

The following examples show the first line of if or while statements:

while ($#argv != 0)While there are arguments ...
if ($today[1] == "Fri")If the first word is "Fri"...
if ($file !~ *.[zZ])

If the file doesn't end with .z or .Z ...

if ($argv[1] =~ chap?)If the first argument is chap followed by a single character...
if (-f $argv[1])If the first argument is a plain file...
if (! -d $tmpdir)If tmpdir is not a directory...

Previous: 5.3 VariablesUNIX in a Nutshell: System V EditionNext: 5.5 Command History csh shell
5.3 VariablesBook Index5.5 Command History csh shell

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System