Performing Arithmetic Calculations
You have probably noticed that all the examples worked with so far have dealt strictly with text, with no mention of how to deal with numeric calculations. That’s because the shell thinks that everything is text; it sees numbers as only ASCII characters. If you need to perform any kind of numeric calculations, you have to use programs outside the shell to do them for you via the backquote operator. The output of these programs can be assigned to a variable or can be printed directly from the echo command.
If all you need to perform are integer calculations, you can use the expression evaluator program, expr, as shown in Table 28.5.
Operator | Description |
---|---|
result=`expr ... |
Get Practical UNIX 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.