Skip to content

Instantly share code, notes, and snippets.

@fremen432
Last active August 6, 2022 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fremen432/64c9f113307ad53af95deb67d3d76d1b to your computer and use it in GitHub Desktop.
Save fremen432/64c9f113307ad53af95deb67d3d76d1b to your computer and use it in GitHub Desktop.
Arithmetic operations in BASH
#!/bin/bash
echo "Enter your first number"
read x
echo "Enter your second number"
read y
(( prod=x*y )) # Multiplication
(( quot=x/y )) # Division
(( sum=x+y )) # Addition
(( dif=x-y )) # Subtraction
(( pow=x**y )) # Power-of
(( mod=x%y )) # Remainder
operations=(
"Add"
"Subtract"
"Multiply"
"Divide"
"Power-of"
"Modulus"
"Exit"
)
select opp in "${operations[@]}"; do
case $opp in
"Add")
echo "$x + $y = $sum"
;;
"Subtract")
echo "$x - $y = $dif"
;;
"Multiply")
echo "$x x $y = $prod"
;;
"Divide")
echo "$x / $y = $quot"
;;
"Power-of")
echo "$x ^ $y = $pow"
;;
"Modulus")
echo "$x % $y = $mod"
;;
"Exit")
echo "User requested exit"
exit
;;
*)
echo "invalid option $REPLY"
;;
esac
done
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment