Find knowledge base article(s) by searching for keywords in the title e.g. type linux in the search box below
Find knowledge base article(s) by browsing the subject categories of articles
Technology quick references, cheatsheets, user manuals etc.
Shop Online through ShopifyLite
Tutorials on various IT applications.
Search Title    (UL:0 |SS:f)

Software >> OS >> Unix >> Linux >> Shell >> How to perform arithmetic such as addition subtraction etc in bash and sh

Example, readh 2 numbers and find the sum, difference, product and quotient

bash

read X
read Y
echo $((X+Y))
echo $((X-Y))
echo $((X*Y))
echo $((X/Y))

sh

read X
read Y
echo `expr $X + $Y`
echo `expr $X - $Y`
expr `expr $X \* $Y`     (note that * is special i.e. wildcard character, hence need to be escaped with backslash)
expr `expr $X \/ $Y`     (note that / is special i.e. wildcard character, hence need to be escaped with backslash)


 

[ © 2008-2021 myfaqbase.com - A property of WPDC Consulting ]