top of page

Shell Script to Perform Arithmetic Operations

Linux shell script allows users to do the arithmetic operation with user input for the operations. In order to achieve this, we will be using a case statement.


echo "enter a and b"
read a
read b
echo "enter operation to be performed"
read op
case $op in
+) c=`expr $a + $b` ;;
-) c=`expr $a - $b` ;;
/) c=`expr $a / $b` ;;
\*) c=`expr $a \* $b` ;;
*) echo "no valid operation specified" ;;
echo Result after performing operation on a and b is
echo $c

Here is the sample output of the above script

shell script to perform arithmetic operations

More ways to use this case statement.

  • You can use case to accept YES or NO, the same way software installation, during the license agreement.

  • You can also use to find File type from the Extension.

Related Posts

Heading 2

Add paragraph text. Click “Edit Text” to customize this theme across your site. You can update and reuse text themes.

bottom of page