PART 2System Interaction and Advanced Techniques

CHAPTER 11Math from the Shell

Mathematical calculations are an important element of writing shell scripts. In the various flavors of UNIX, there are multiple ways to perform just about any task, and mathematical tasks are no exception.

Although there are many types of mathematical computations, I've limited the discussion in this chapter to the basic operations of addition, subtraction, multiplication, and division, plus remainder, exponentiation, and trigonometric functions. The examples make use of the variables a, b, c, and d.

One note on division: some programming languages don't perform integer division like you might expect from using your traditional calculator. There are two parts to ...

Get Expert Shell Scripting 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.