Evaluating Expressions with expr

Unix also provides expr, which you can use for evaluating expressions. (In this use, the term expressions refers to the mathematical, logical, scientific meaning of the word.) In addition to evaluating mathematical expressions, you can evaluate darn near anything else. The expr utility is often used in shell scripts—and you’ll probably find the most value in expr in that context—but it works just fine at the command line, too, as shown in Code Listing 15.3.

Code Listing 15.3. Using the expr utility, evaluating the value or the truth (or lack thereof) of expressions is straightforward.
[jdoe@frazz jdoe]$ expr 3 \* 4
12
[jdoe@frazz jdoe]$ expr 5 % 3
2
[jdoe@frazz jdoe]$ a=$PWD; b=$HOME; expr$a = $b 1 [jdoe@frazz ...

Get Unix Third Edition: Visual Quickstart Guide 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.