0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Create Simple Calculator

You can create a simple calculator in Bash by reading two numbers and an operator, then using case to perform operations like +, -, *, and /; for example: read -p "Enter first number: " a; read -p "Enter operator (+ - * /): " op; read -p "Enter second number: " b; case $op in +) echo $((a + b)) ;; -) echo $((a - b)) ;; '*') echo $((a * b)) ;; /) echo $((a / b)) ;; esac.
📋

Examples

Input5 + 3
Output8
Input10 / 2
Output5
Input7 * 0
Output0
🧠

How to Think About It

To build a simple calculator in Bash, first get two numbers and an operator from the user. Then, use a case statement to decide which arithmetic operation to perform based on the operator. Finally, calculate and display the result.
📐

Algorithm

1
Prompt the user to enter the first number and store it.
2
Prompt the user to enter an operator (+, -, *, /) and store it.
3
Prompt the user to enter the second number and store it.
4
Use a case statement to check the operator and perform the corresponding arithmetic operation.
5
Print the result of the calculation.
💻

Code

bash
read -p "Enter first number: " a
read -p "Enter operator (+ - * /): " op
read -p "Enter second number: " b
case $op in
  +) result=$((a + b)) ;;
  -) result=$((a - b)) ;;
  '*') result=$((a * b)) ;;
  /) 
    if [ "$b" -eq 0 ]; then
      echo "Error: Division by zero"
      exit 1
    fi
    result=$((a / b)) ;;
  *) echo "Invalid operator"; exit 1 ;;
esac
echo "Result: $result"
🔍

Dry Run

Let's trace the input '8 * 7' through the code

1

Read first number

a = 8

2

Read operator

op = *

3

Read second number

b = 7

4

Perform calculation

Since op is '*', calculate 8 * 7 = 56

5

Print result

Output: Result: 56

StepVariableValue
1a8
2op*
3b7
4result56
💡

Why This Works

Step 1: Reading inputs

The script uses read -p to get two numbers and an operator from the user.

Step 2: Choosing operation

A case statement checks the operator and selects the correct arithmetic operation.

Step 3: Calculating and outputting

The script calculates the result using arithmetic expansion $(( )) and prints it.

🔄

Alternative Approaches

Using expr command
bash
read -p "Enter first number: " a
read -p "Enter operator (+ - \* /): " op
read -p "Enter second number: " b
result=$(expr $a $op $b)
echo "Result: $result"
Uses external expr command; less efficient but works on older shells.
Using bc for floating point
bash
read -p "Enter first number: " a
read -p "Enter operator (+ - * /): " op
read -p "Enter second number: " b
result=$(echo "$a $op $b" | bc -l)
echo "Result: $result"
Supports floating point calculations but requires bc installed.

Complexity: O(1) time, O(1) space

Time Complexity

The script performs a fixed number of operations regardless of input size, so it runs in constant time O(1).

Space Complexity

It uses a few variables to store inputs and results, so space usage is constant O(1).

Which Approach is Fastest?

Using Bash arithmetic expansion is fastest and simplest; using external commands like expr or bc adds overhead.

ApproachTimeSpaceBest For
Bash arithmetic expansionO(1)O(1)Simple integer calculations
expr commandO(1)O(1)Compatibility with older shells
bc commandO(1)O(1)Floating point calculations
💡
Always check for division by zero to avoid errors in your calculator script.
⚠️
Beginners often forget to quote variables or escape the * operator, causing syntax errors.