## Performing Basic Arithmetic Operations

Simple arithmetic operations like addition, subtraction, division, and multiplication can be performed using the ‘bc’ command. The syntax for applying simple binary operators in bash using the ‘bc’ command is as follows.

In this section, we will use the ‘bc’ command to perform simple arithmetic operations.

[email protected]:~$ num2=1.223353

[email protected]:~$ echo “$num1+$num2” | bc

3.576578

[email protected]:~$ echo “$num1-$num2” | bc

1.129872

[email protected]:~$ echo “$num1*$num2” | bc

2.878824

[email protected]:~$ echo “$num1/$num2” | bc

1

In the above example, while performing division, we got the result without decimal points. To get the result up to ‘n’ decimal points, we have to set the scale value to ‘n’ as shown in the following example.

Alternatively, we can use the ‘-l’ flag and the ‘bc’ command to get decimal output.

The ‘bc’ command can also be used to perform modulus division and calculate the power of a number in bash scripts.

## Performing Advanced Arithmetic Operations

So far, we have used the ‘bc’ command to perform some basic arithmetic operations like addition, subtraction, multiplication, etc., now; in this section, we will use the ‘bc’ command to perform some advanced arithmetic operations. We will discuss how we can use comparison operators, logical or boolean operators, advanced mathematical functions, and conditional statements in bash using the ‘bc’ command.

## Comparison Operators

Comparison operators take two numbers, compare them and then return **1** or **0** depending upon the comparison. If the comparison is true, then the result is **TRUE(1)**; otherwise, it is **FALSE(0)**. Following are some examples of comparison operators.

**num1 > num2:**This comparison will return**1**if the**num1**is greater than the**num2**.**num1 < num2:**The result will be**1**if the**num1**is less than the**num2**.**num1 <= num2:**The result will be**1**if the**num1**is less than or equal to the**num2.****num1 >= num2:**The result will be**1**if the**num1**is greater than or equal to the**num2**.**num1 == num2 :**The result will be**1**if the**num1**is equal to the**num2**.**num1 != num2:**The result will be**1**if both the numbers are not equal.

Following are some examples of comparison operators used along with the ‘bc’ command.

1

[email protected]:~$ echo “4!=4” | bc

0

[email protected]:~$ echo “2>5” | bc

0

[email protected]:~$ echo “4<=4” | bc

1

## Boolean Operators

Boolean or Logical operators are used in conditional statements to perform some logical decisions. Following are the three basic logical operators.

**stat1 && stat2:**This will return**1**if both the statements are**non-zero**.**stat1 || stat2:**This will return**1**if any of the statements is**non-zero**.**! stat:**This will return**1**if the statement is**non-zero**and vice versa.

The following examples illustrate how logical operators are used with the ‘bc’ command.

0

[email protected]:~$ echo “-1 || 0” | bc

1

[email protected]:~$ echo “! 0” | bc

1

## Conditional Statements

Conditional statements are used to execute specific commands depending upon the condition applied. The applied condition in the conditional statement involves logical and comparison operators. Following is the example of conditional statements with the ‘bc’ command.

[email protected]:~$ b=20

[email protected]:~$ echo ‘ if(a>b) print “a is greater” else print “b is greater” ‘ | bc -l

b is greater

In the above example, the statement checks if a is greater than b or not. If a is greater than b, it will print “a is greater”; otherwise, it will print “b is greater.” We can apply any condition using boolean and comparison operators in the above example.

## Mathematical Functions

The ‘bc’ command also provides some built-in mathematical functions which we can use without defining them. Following are some essential functions used with the ‘bc’ command in bash.

**s(x):**returns sine of x where x is in radians**c(x):**returns cosine of x where x is in radians**a(x):**returns arctangent of x and the result is in radians**sqrt(x):**returns square root of x. It causes runtime error when x is negative**l(x):**returns natural log of the x.

These functions can be used with the ‘bc’ command, as shown in the following examples.

[email protected]:~$ echo “s($pi/2)” | bc -l

1

[email protected]:~$ echo “c($pi/2)” | bc -l

0

[email protected]:~$ echo “a(1)” | bc -l

0.7854

The square root of a number can be calculated in bash using the ‘bc’ command, as shown in the following figure.

While trying to calculate the square root of a negative number, the shell will throw a runtime error.

Runtime error (func=(main), adr=4): Square root of a negative number

The Natural Logarithm of a number can be calculated in bash using the ‘bc’ command as follows.

## Conclusion

While writing automation scripts in bash, sometimes we need advanced mathematical functions and logical operators to execute commands. The ‘bc’ command provides many advanced mathematical functions and operators to perform high-level arithmetic calculations. This tutorial discussed using the ‘bc’ command to perform advanced arithmetic operations in bash.