Thursday, December 26, 2024
Google search engine
HomeGuest BlogsBash Math Operations (Bash Arithmetic) Explained

Bash Math Operations (Bash Arithmetic) Explained

Introduction

Math and arithmetic operations are essential in Bash scripting. Various automation tasks require basic arithmetic operations, such as converting the CPU temperature to Fahrenheit. Implementing math operations in Bash is simple and very easy to learn.

This guide teaches you how to do basic math in Bash in various ways.

bash math bash arithmetic explainedbash math bash arithmetic explained

Prerequisites

Why Do You Need Math in Bash Scripting?

Although math is not the primary purpose of Bash scripting, knowing how to do essential calculations is helpful for various use cases.

Common use cases include:

  • Adding/subtracting/multiplying/dividing numbers.
  • Rounding numbers.
  • Incrementing and decrementing numbers.
  • Converting units.
  • Floating-point calculations.
  • Finding percentages.
  • Working with different number bases (binary, octal, or hexadecimal).

Depending on the automation task, basic math and arithmetic in Bash scripting help perform a quick calculation, yielding immediate results in the desired format.

Bash Math Commands and Methods

Some Linux commands allow performing basic and advanced calculations immediately. This section shows basic math examples with each method.

Arithmetic Expansion

The preferable way to do math in Bash is to use shell arithmetic expansion. The built-in capability evaluates math expressions and returns the result. The syntax for arithmetic expansions is:

$((expression))

The syntax consists of:

  • Compound notation (()) which evaluates the expression.
  • The variable operator $ to store the result.

Note: The square bracket notation ( $[expression] ) also evaluates an arithmetic expression, and should be avoided since it is deprecated.

For example, add two numbers and echo the result:

echo $((2+3))
bash arithmetic expansion addition terminal outputbash arithmetic expansion addition terminal output

The arithmetic expansion notation is the preferred method when working with Bash scripts. The notation is often seen together with if statements and for loops in Bash.

awk Command

The awk command acts as a selector for pattern expressions. For example, to perform addition using the awk command, use the following example statement:

awk 'BEGIN { x = 2; y = 3; print "x + y = "(x+y) }'
awk addition terminal outputawk addition terminal output

For variables x = 2 and y = 3, the output prints x + y = 5 to the console.

bc Command

The bc command (short for basic calculator) is a command-line utility that renders the bc language. The program runs as an interactive program or takes standard input to perform arbitrary precision arithmetic.

Pipe an equation from standard input into the command to fetch results. For example:

echo "2+3" | bc
bc addition terminal outputbc addition terminal output

The output prints the calculation result.

dc Command

The dc command (short for desk calculator) is a calculator utility that supports reverse Polish notation. The program takes standard input and supports unlimited precision arithmetic.

Pipe a standard input equation into the command to fetch the result. For example:

echo "2 3 + p" | dc
dc addition terminal outputdc addition terminal output

The p in the equation sends the print signal to the dc command.

declare Command

The Bash declare command allows integer calculations. To use declare for calculations, add the -i option. For example:

declare -i x=2 y=3 z=x+y

Echo each variable to see the results:

echo $x + $y = $z
declare addition terminal outputdeclare addition terminal output

The output prints each variable to the console.

expr Command

The expr command is a legacy command line utility for evaluating integer arithmetic. An example expr command looks like the following:

expr 2 + 3
expr addition terminal outputexpr addition terminal output

Separate numbers and the operation sign with spaces and run the command to see the calculation result.

factor Command

The factor command is a command-line utility that prints the factors for any positive integer, and the result factorizes into prime numbers.

For example, to print the factors of the number 100, run:

factor 100
factor 100 terminal outputfactor 100 terminal output

The output prints the factored number.

let Command

The Bash let command performs various arithmetic, bitwise and logical operations. The built-in command works only with integers. The following example demonstrates the let command syntax:

let x=2+3 | echo $x
let addition terminal outputlet addition terminal output

The output prints the results.

test Command

The test command in Linux evaluates conditional expressions and often pairs with the Bash if statement. There are two variations for the test syntax:

test 2 -gt 3; echo $?
test comparison terminal outputtest comparison terminal output

Or alternatively:

[ 2 -gt 3 ]; echo $?
test bracket comparison terminal outputtest bracket comparison terminal output

The test command evaluates whether two is greater than (-gt) three. If the expression is true, the output is zero (0), or one (1) if false.

Bash Arithmetic Operators

Bash offers a wide range of arithmetic operators for various calculations and evaluations. The operators work with the let, declare, and arithmetic expansion.

Below is a quick reference table that describes Bash arithmetic operators and their functionality.

Syntax Description
++x, x++ Pre and post-increment.
--x, x-- Pre and post-decrement.
+, -, *, / Addition, subtraction, multiplication, division.
%, ** (or ^) Modulo (remainder) and exponentiation.
&&, ||, ! Logical AND, OR, and negation.
&, |, ^, ~ Bitwise AND, OR, XOR, and negation.
<=, <, >, => Less than or equal to, less than, greater than, and greater than or equal to comparison operators.
==, != Equality and inequality comparison operators.
= Assignment operator. Combines with other arithmetic operators.

How to Do Math in Bash

Bash offers different ways to perform math calculations depending on the type of problem.

Below are examples of some common problems which use Bash math functionalities or commands as a solution. Most examples use the Bash arithmetic expansion notation. The section also covers common Bash math errors and how to resolve them.

Math with Integers

The arithmetic expansion notation is the simplest to use and manipulate with when working with integers. For example, create an expression with variables and calculate the result immediately:

echo $((x=2, y=3, x+y))
bash arithmetic expansion variables terminal outputbash arithmetic expansion variables terminal output

To evaluate multiple expressions, use compound notation, store each calculation in a variable, and echo the result. For example:

((x=2, y=3, a=x+y, b=x*y, c=x**y)); echo $a, $b, $c
bash multiple equations terminal outputbash multiple equations terminal output

When trying to divide, keep the following in mind:

1. Division by zero (0) is impossible and throws an error.

bash division by zero error terminal outputbash division by zero error terminal output

2. Bash arithmetic expansion does not support floating-point arithmetic. When attempting to divide in this case, the output shows zero (0).

non-integer result terminal outputnon-integer result terminal output

The result of integer division must be an integer.

Incrementing and Decrementing

Bash arithmetic expansion uses C-style integer incrementing and decrementing. The operator for incrementing or decrementing is either before or after the variable, yielding different behavior.

If the operator is before the variable (++x or --x), the increment or decrement happens before value assignment. To see how pre-incrementing works, run the following lines:

number=1
echo $((++number))
c-style pre-increment terminal outputc-style pre-increment terminal output

The variable increments, and the new value is immediately available.

If the operator is after the variable (x++ or x--), the increment or decrement happens after value assignment. To see how post-incrementing works, run the following:

number=1
echo $((number++))
echo $number
c-style post-increment terminal outputc-style post-increment terminal output

The variable stays the same and increments in the following use.

Floating-point Arithmetic

Although Bash arithmetic expansion does not support floating-point arithmetic, there are other ways to perform such calculations. Below are four examples using commands or programming languages available on most Linux systems.

1. Using awk for up to 6 decimal places:

awk 'BEGIN { x = 2.3; y = 3.2; print "x * y = "(x * y) }'
awk bash floating point arithmetic terminal outputawk bash floating point arithmetic terminal output

2. Using bc with the -l flag for up to 20 decimal places:

echo "2.3 * 3.2" | bc -l
bc -l floating point arithmetic terminal outputbc -l floating point arithmetic terminal output

3. Using Perl for up to 20 decimal places:

perl -e 'print 2.3*3.2'
perl floating point arithmetic terminal outputperl floating point arithmetic terminal output

Perl often comes preinstalled in Linux systems.

4. Using printf and arithmetic expansion to convert a fraction to a decimal:

printf %.<precision>f "$((10**<multiplier> * <fraction>))e-<multiplier>"

Precision dictates how many decimal places, whereas the multiplier is a power of ten. The number should be lower than the multiplier. Otherwise, the formula puts trailing zeros in the result.

For example, convert 1/3 to a decimal with precision two:

printf %.2f "$((10**3 * 1/3))e-3"
printf bash arithmetic expansion floating point terminal outputprintf bash arithmetic expansion floating point terminal output

Avoid this method for precise calculations and use it only for a small number of decimal places.

Calculating a Percentage and Rounding

Below are two ways to calculate a percentage in Bash.

1. Use printf with arithmetic expansion.

printf %.2f "$((10**4 * part/total))e-4"%

For example, calculate what percent 40 is from 71:

printf %.2f%% "$((10**4 * 40/71))e-4"%
printf percent calculation terminal outputprintf percent calculation terminal output

The precision is limited to two decimal places, and the answer always rounds down.

2. Use awk with printf for better precision:

awk 'BEGIN { printf "%.2f%%", (part/total*100) }'

For instance, calculate how many percent is 40 from 71 with:

awk 'BEGIN { printf "%.2f%%", (40/71*100) }'
awk percent calculation terminal outputawk percent calculation terminal output

The answer rounds up if the third decimal place is higher than five, providing better accuracy.

Finding a Factorial in the Shell

To calculate a factorial for any number, use a recursive Bash function.

For small numbers, Bash arithmetic expansion works well:

factorial () { 
    if (($1 > 1))
    then
        echo $(( $( factorial $(($1 - 1)) ) * $1 ))
    else

        echo 1
        return
    fi
}

To check the factorial for a number, use the following syntax:

factorial 5
bash arithmetic expansion factorial function terminal outputbash arithmetic expansion factorial function terminal output

The method is slow and has limited precision (up to factorial 20).

For higher precision, faster results, and larger numbers, use the bc command. For example:

echo 'define factorial(x) {if (x>1){return x*factorial(x-1)};return 1}
 factorial(<number>)' | bc

Replace <number> with the factorial number to calculate. For example, to find the factorial of 50, use:

echo 'define factorial(x) {if (x>1){return x*factorial(x-1)};return 1} factorial(50)' | bc
bc factorial function terminal outputbc factorial function terminal output

The output prints the calculation result to the terminal.

Creating a Bash Calculator Function

Create a simple Bash calculator function with the following code:

calculate() { printf "%s\n" "$@" | bc -l; }
calculate bc function terminal outputcalculate bc function terminal output

The function takes user input and pipes the equation into the bc command.

Alternatively, to avoid using programs, use Bash arithmetic expansion in a function:

calculate() { echo $(("$@")); }
calculate bash arithmetic expansion function terminal outputcalculate bash arithmetic expansion function terminal output

Keep the arithmetic expansion limitations in mind. Floating-point arithmetic is not available with this function.

Save the function into the .bashrc file to always have the function available in the shell.

Using Different Arithmetic Bases

By default, Bash arithmetic expansion uses base ten numbers. To change the number base, use the following format:

base#number

Where base is any integer between two and 64.

For example, to do a binary (base 2) calculation, use:

echo $((2#1010+2#1010))
bash binary math terminal outputbash binary math terminal output

Octal (base 8) calculations use a 0 prefix as an alias. For example:

echo $((010+010))
bash octal math terminal outputbash octal math terminal output

Hexadecimal (base 16) calculations allow using 0x as a base prefix. For example:

echo $((0xA+0xA))
bash hexadecimal math terminal outputbash hexadecimal math terminal output

The output prints the result in base ten for any calculation.

Convert Units

Create a simple Bash script to convert units:

1. Open a text editor, such as Vim, and create a convert.sh script. For example:

vim convert.sh

2. Paste the following code:

#!/bin/bash

## Program for feet and inches conversion

echo "Enter a number to be converted:"

read number

echo $number feet to inches:
echo "$number*12" | bc -l

echo $number inches to feet:
echo "$number/12" | bc -l

The program uses Bash read to take user input and calculates the conversion from feet to inches and from inches to feet.

3. Save the script and close:

:wq

4. Run the Bash script with:

. convert.sh
convert.sh bash script terminal outputconvert.sh bash script terminal output

Enter a number and see the result. For different conversions, use appropriate conversion formulas.

Solving “bash error: value too great for base”

When working with different number bases, stay within the number base limits. For example, binary numbers use 0 and 1 to define numbers:

echo $((2#2+2#2))

Attempting to use 2#2 as a number outputs an error:

bash: 2#2: value too great for base (error token is "2#2")
bash value too great for base error terminalbash value too great for base error terminal

The number is not the correct format for binary use. To resolve the error, convert the number to binary to perform the calculation correctly:

echo $((2#10+2#10))

The binary number 10 is 2 in base ten.

Solving “syntax error: invalid arithmetic operator”

The Bash arithmetic expansion notation only works for integer calculations. Attempt to add two floating-point numbers, for example:

echo $((2.1+2.1))

The command prints an error:

bash: 2.1+2.1: syntax error: invalid arithmetic operator (error token is ".1+2.1")
bash syntax error invalid arithmetic operator terminal outputbash syntax error invalid arithmetic operator terminal output

To resolve the error, use regular integer arithmetic or a different method to calculate the equation.

Solving “bash error: integer expression expected”

When comparing two numbers, the test command requires integers. For example, try the following command:

[ 1 -gt 1.5 ]

The output prints an error:

bash: [: 1.5: integer expression expected
bash integer expression expected error terminalbash integer expression expected error terminal

Resolve the error by comparing integer values.

Note: Learn about mathematical operations in Python by exploring two methods to calculate the power of a number in our guide Python Power Operator and Function.

Conclusion

You know how to do Bash arithmetic and various calculations through Bash scripting.

For more advanced scientific calculations, use Python together with SciPy, NumPy, and other libraries.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments