Skip to content
Home » Bash Arithmetic 101: The Beginner’s Guide to Calculating in the Shell

Bash Arithmetic 101: The Beginner’s Guide to Calculating in the Shell

Bash is a Unix shell, which is a command-line interface for interacting with an operating system. One of the useful features of Bash is the ability to perform arithmetic operations and manipulate numbers directly within the shell. In this blog, we will discuss how to perform arithmetic operations in Bash and provide examples of how to use them to increase productivity and efficiency.

Addition

The addition operator (+) adds two numbers together and returns the sum. It can also be used to concatenate strings.

Subtraction

The subtraction operator (-) subtracts one number from another and returns the difference.

Multiplication

The multiplication operator (*) multiplies two numbers together and returns the product.

Division

The division operator (/) divides one number by another and returns the quotient.

Modulo

The modulo operator (%) returns the remainder of a division operation. It can also be used to determine whether a number is even or odd.

Examples

Here are some examples of arithmetic operations in Bash using the $((expression)) syntax:

# Addition
result=$((2 + 2))
echo $result # Output: 4

# Subtraction
result=$((10 - 5))
echo $result # Output: 5

# Multiplication
result=$((3 * 4))
echo $result # Output: 12

# Division
result=$((10 / 2))
echo $result # Output: 5

# Exponentiation
result=$((2 ** 3))
echo $result # Output: 8

You can also use variables in your arithmetic operations:

a=5
b=10
result=$((a + b))
echo $result # Output: 15

Something that many people don’t know about Bash arithmetic is that you can use the let command to perform arithmetic operations. For example:

let result=2+2
echo $result # Output: 4

The let command is often used in looping constructs, as it allows you to perform arithmetic operations and assignment in a single step.

Calculate the difference between two timestamps:

start_time=$(date +%s)
# Run a long-running command here
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo "Elapsed time: $elapsed_time seconds"

Iterate through a range of numbers:

for i in $(seq 1 10); do
  echo $i
done

Generate random numbers:

random_number=$((RANDOM % 10 + 1))
echo $random_number

Perform batch renaming of files using a counter:

counter=1
for file in *.jpg; do
  mv "$file" "itvraag.nl_$counter.jpg"
  counter=$((counter + 1))
done

Use the modulo operator (%) to check if a number is even or odd:

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

Challenge

Now that you’ve learned about Bash arithmetic, try using it to solve the following problem:

Write a Bash script that generates a random number between 1 and 10, and then prompts the user to guess the number. If the user guesses correctly, the script should print “You guessed it!” and exit. If the user guesses incorrectly, the script should print “Wrong!” and prompt the user to try again until they guess correctly.

Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

three + 18 =