Shell script factors are naturally treated as strings, not numbers, which adds some intricacy to doing math in shell script. To keep with script programming worldview and take into account better numerical help, dialects such Perl or Python would be more qualified when math is wanted. Nonetheless, it is feasible to do math with shell script. Truth be told, throughout the long term, different offices have been added to Unix to help working with numbers.

Note As we will see, a portion of the orders used to work with math are a little meticulous with regards to things like spaces around administrators.

5.9.1. proclaim

You might review, that when the reading material presented the announce explanation, it said that it isn’t constantly required. So what do you get by announcing a variable to be a whole number? The accompanying model outlines that an announced number isn’t treated as a string.

$ n=6/3

$ reverberation $n

6/3

$ pronounce – I n

$ n=6/3

$ reverberation $n

2

At the point when you needn’t bother with the pronounce articulation is the point at which you will utilize a program or inherent order to assess a mathematical assertion.

5.9.2. expr

An old Unix program that can assess math is expr. expr became famous in the times of the Bourne Shell, which didn’t uphold math. With Bash and Korn shell, it is by and large not required. Since it is an order, order replacement is required. We are as yet regarding the variable as a string. As may be obvious, it is exacting with regards to spaces.

$ z=5

$ z=’expr $z+1′ – – – – Need spaces around + sign.

$ reverberation $z

5+1

$ z=’expr $z + 1′

$ reverberation $z

6

5.9.3. let

A Bash and Korn shell worked in order for math is let. As may be obvious, it is likewise a little fastidious with regards to spaces, yet it needs something contrary to what expr needed. let likewise loosens up the ordinary rule of requiring a $ before factors to be perused.

$ let z=5

$ reverberation $z

5

$ let z=$z+1

$ reverberation $z

6

$ let z=$z + 1 # – – – Spaces around + sign are terrible with let

-slam: let: +: grammar blunder: operand expected (mistake token is “+”)

$let z=z+1 # – – – look Mom, no $ to peruse a variable.

$reverberation $z

7

5.9.4. Slam Arithmetic

With the BASH shell, entire number-crunching articulations might be set inside twofold enclosure. This structure is more lenient with regards to spaces.

$ ((e=5))

$ reverberation $e

5

$ (( e = e + 3 ))

$ reverberation $e

8

$ (( e=e+4 )) # – – spaces or no spaces, it doesn’t make any difference

$ reverberation $e

12

Number-crunching Operators

+ – Expansion, subtration

++ — Increase, decrement

* / % Increase, division, remaining portion

** Exponentiation

Mathematical boolean articulations in Control Constructs are likewise communicated utilizing twofold enclosure.

on the off chance that (( x > y ));,

reverberation “x > y”

fi

Intelligent and Boolean Operators

<= >= < > Not exactly or equivalent, more noteworthy than or equivalent, not exactly, more prominent than

== != Equivalent, not equivalent

! Legitimate NOT

&& Legitimate AND

|| Legitimate OR

5.9.5. bc

Imagine a scenario where you need to do math with drifting point numbers or you have some genuinely muddled math to do. Neither type of let, upholds drifting point numbers. The bc order is required. Be that as it may, you need to regard the factors as strings.

Here is the thing that happens when we attempt to do drifting point math with the shell:

$let r=3.5

-slam: let: r=3.5: grammar mistake in articulation (blunder token is “.5”)

$(( r = 3.5 ))

-slam: ((: r = 3.5 : language structure mistake in articulation (blunder token is “.5 “)

bc

A discretionary accuracy mini-computer language. bc may either be run intuitively, or as a shell script order. In intuitive mode, type cntrl-d (EOF) to exit.

Abstract

bc

bc EXPRESSION

Here are a few models:

$ bc

bc 1.06

Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

This is free programming with ABSOLUTELY NO WARRANTY.

For subtleties type ‘guarantee’.

3 + 2

5

obase=2

12

1100

<cntrl-d>

Make sure to type cntrl-d (EOF) to exit from intelligent mode.

$r=3.5

$s=’echo “$r + 2.2” | bc’

$reverberation $s

5.7

$ z = ‘reverberation $z + 1 | bc’

-slam: z: order not found

# — spaces around = sign are terrible

(shell thing, not bc)

$ z=’echo “$z + 1” | bc’

$ reverberation $z

8

$ z=’echo “$z+1” | bc’ – – spaces don’t make any difference with bc

$ reverberation $z

9

5.9.6. Numeric Boolean articulations

Assuming BASH twofold enclosure are not utilized, then, at that point, the test order should be utilized to think about number factors. See test.

INTEGER1 – eq INTEGER2

INTEGER1 is equivalent to INTEGER2

INTEGER1 – ge INTEGER2

INTEGER1 is more prominent than or equivalent to INTEGER2

INTEGER1 – gt INTEGER2

INTEGER1 is more noteworthy than INTEGER2

INTEGER1 – le INTEGER2

INTEGER1 is not exactly or equivalent to INTEGER2

INTEGER1 – lt INTEGER2

INTEGER1 is under INTEGER2

INTEGER1 – ne INTEGER2

INTEGER1 isn’t equivalent to INTEGER2

In other words, the accompanying two in the event that assertions are indistinguishable:

assuming (( x < y ));,

proclamations

fi

on the off chance that [ $x – lt $y ];

articulations

fi

Also for intelligent articulations utilizing drifting point math, the bc order returns 1 for sensible genuine articulations and 0 for bogus articulations, accordingly testing for aftereffect of 1 accomplishes a coherent articulation:

on the off chance that [ $( reverberation “$t < 3.4” | bc ) – eq 1 ]; then, at that point,

proclamations

fi