Chapter 7. Performing Arithmetic Operations in Shell Scripts

In the last chapter, you learned about debugging, the here operator, interactive Shell scripts for taking input from the keyboard, and file handling.

In this chapter, we will cover the following arithmetic operations topics:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus

We can perform arithmetic operations in various ways, such as using declare, let, expr, and arithmetic expressions. You will also learn about representing numbers in different bases, such as binary, octal, and hex.

Using a command declare for arithmetic

Whenever we declare any variable, by default, this variable stores the string type of data. We cannot do arithmetic operations on them. We can declare a variable as an integer ...

Get Learning Linux 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.