• Arun Kumar

Shell Script to Simple Calculator

Linux shell script allows users to create a calculation to perform the calculation as they perform in the calculator. In order to achieve this, we will be using a case statement to perform the calculation as per the user input.


vi simple_calculator.sh
# !/bin/bash 
  
# Take user Input 
echo "Enter Two numbers : "
read a 
read b 
  
# Input type of operation 
echo "Enter Choice :"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read ch 
  
# Switch Case to perform 
# calulator operations 
case $ch in
  1)res=`echo $a + $b | bc` 
  ;; 
  2)res=`echo $a - $b | bc` 
  ;; 
  3)res=`echo $a \* $b | bc` 
  ;; 
  4)res=`echo "scale=2; $a / $b" | bc` 
  ;; 
esac
echo "Result : $res" 

Here is the sample output of the above script

shell script to simple calculator

More we to make a calculator

  • You can use else if to perform the calculation as per user input.

  • You can also use the While loop to perform the same operation.

2,177 views

Recent Posts

See All