Skip to main content
ubuntuask.com

Back to all posts

How to Perform Arithmetic Operations In Bash?

Published on
4 min read
How to Perform Arithmetic Operations In Bash? image

Best Bash Scripting Tools to Buy in October 2025

1 Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

  • AFFORDABLE PRICES FOR QUALITY READS-SAVE WHILE YOU READ!
  • GENTLY USED BOOKS: ENJOY A GREAT STORY WITHOUT THE FULL PRICE.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY CHOOSING SECONDHAND BOOKS.
BUY & SAVE
$22.15 $44.99
Save 51%
Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))
2 bash Idioms: Write Powerful, Flexible, Readable Shell Scripts

bash Idioms: Write Powerful, Flexible, Readable Shell Scripts

BUY & SAVE
$32.49 $55.99
Save 42%
bash Idioms: Write Powerful, Flexible, Readable Shell Scripts
3 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
4 bash Cookbook: Solutions and Examples for bash Users (Cookbooks (O'Reilly))

bash Cookbook: Solutions and Examples for bash Users (Cookbooks (O'Reilly))

  • QUALITY ASSURANCE: EVERY BOOK IS THOROUGHLY INSPECTED FOR CONDITION.
  • AFFORDABLE PRICING: SAVE MONEY WITH COMPETITIVELY PRICED USED BOOKS.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY PURCHASING PRE-OWNED BOOKS.
BUY & SAVE
$101.07
bash Cookbook: Solutions and Examples for bash Users (Cookbooks (O'Reilly))
5 Pro Bash: Learn to Script and Program the GNU/Linux Shell

Pro Bash: Learn to Script and Program the GNU/Linux Shell

BUY & SAVE
$42.01 $54.99
Save 24%
Pro Bash: Learn to Script and Program the GNU/Linux Shell
6 Bash Pocket Reference: Help for Power Users and Sys Admins

Bash Pocket Reference: Help for Power Users and Sys Admins

BUY & SAVE
$17.60 $24.99
Save 30%
Bash Pocket Reference: Help for Power Users and Sys Admins
7 Bash in easy steps

Bash in easy steps

BUY & SAVE
$10.05 $15.99
Save 37%
Bash in easy steps
+
ONE MORE?

In Bash scripting, you can perform arithmetic operations using various built-in operators and commands. Here are the basic arithmetic operations you can perform:

  1. Addition: Use the + operator to add numbers together. For example: sum=$((4 + 3))
  2. Subtraction: Use the - operator to subtract one number from another. For example: difference=$((9 - 5))
  3. Multiplication: Use the * operator to multiply numbers. For example: product=$((2 * 6))
  4. Division: Use the / operator to divide one number by another. For example: quotient=$((10 / 2))
  5. Modulo: Use the % operator to obtain the remainder from division. For example: remainder=$((15 % 4))
  6. Exponentiation: Use the ** operator to raise a number to a power. For example: power=$((2 ** 3))

Apart from the operators, you can use the expr command for more complex arithmetic expressions. For example:

result=$(expr 5 + 2 \* 3)

Here, the backslash \ is used to escape the * operator, as it has a special meaning in Bash.

You can also use arithmetic expansion by enclosing the arithmetic expression within $((...)). For example:

total=$((3 + (6 * 2)))

This allows embedding complex expressions within the arithmetic operation.

Remember to use proper syntax and escape characters as necessary to ensure the desired calculations in Bash.

How to determine if a number is less than or equal to another in Bash?

In bash, you can use the [ or [[ command to evaluate conditions and determine if a number is less than or equal to another. Here's an example:

#!/bin/bash

Assign two numbers to variables

number1=5 number2=10

Using [ command

if [ $number1 -le $number2 ]; then echo "$number1 is less than or equal to $number2" else echo "$number1 is greater than $number2" fi

Using [[ command

if [[ $number1 -le $number2 ]]; then echo "$number1 is less than or equal to $number2" else echo "$number1 is greater than $number2" fi

In this example, the -le operator is used within the conditional statement (if) to check if $number1 is less than or equal to $number2. If the condition is true, the first echo statement will be executed; otherwise, the second echo statement will be executed.

Note: The variable names (number1 and number2) correspond to the values you want to compare. Make sure to set their values before using them in the conditional statements.

What is the command to check if a number is odd in Bash?

In Bash, you can check if a number is odd using the modulus operator %. The following is an example of a conditional statement that checks if a number is odd in Bash:

#!/bin/bash

number=5

if ((number % 2 != 0)); then echo "Number is odd" else echo "Number is even" fi

In this example, the number 5 is assigned to the variable number. The conditional statement checks if the number modulus 2 is not equal to 0. If the condition evaluates to true, the number is odd and the script prints "Number is odd". Otherwise, if the condition is false, it is even and the script prints "Number is even".

What is the shortcut for exponentiation in Bash?

In Bash, the shortcut for exponentiation is not available as a native programming operator. However, you can use the bc command-line calculator to perform exponentiation. Here's an example:

# Using bc for exponentiation result=$(echo "2^3" | bc) echo $result

This will output 8, as 2^3 represents the exponentiation of 2 raised to the power of 3.

How to add two numbers in Bash?

In Bash, you can use the expr command or the arithmetic expansion $(()) to add two numbers. Here are two ways to do it:

  1. Using expr command:

#!/bin/bash

Assign the numbers to variables

num1=5 num2=10

Use the expr command to add the numbers

result=$(expr $num1 + $num2)

Print the result

echo "The sum is: $result"

  1. Using arithmetic expansion $(()):

#!/bin/bash

Assign the numbers to variables

num1=5 num2=10

Use arithmetic expansion to add the numbers

result=$((num1 + num2))

Print the result

echo "The sum is: $result"

Both methods will give you the sum of the two numbers, which in this case is 15.