Arithmetic in Shell Script

The arithmetic operations can be done in shell script using expr command

The following program shows the various arithmetic operations


#!/bin/sh

a=10 b=5

echo `expr $a + $b`

echo `expr $a -  $b`

echo `expr $a * $b`

echo `expr $a / $b`

x=10.5  y=5.5

echo `expr $x+$y|bc`

echo `expr $x-$y|bc`

echo `expr $x*$y|bc`

The output of the program looks like this

15

5

50

2

16.0

5.0

57.7

The bc command is used to perform real arithmetic operations. Here we have piped the result of echo to bc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s