Mathmatical Functions and Linux Shells

How to incorporate Maths into the Command Line

© Mark Alexander Bain

Aug 28, 2008
The Korn shell has mathematical functions, Mark Alexander Bain
Bash is the default shell for Linux and is very powerful. However, it lacks in-built mathematical functions - something that that the Korn shell does have.

Anyone using the Linux command line or developing scripts will, by default, be using the Bourne-Again shell (Bash); and when using this shell they will be able to carry out simple mathematical calculations, such as:

(( a = 3 ))

(( b = 9 ))

(( c = a * b ))

echo $c # output will be 27

However, the Bash script writer will soon find that they will see errors when they try something like:

(( d = sqrt(c) )) # an error will occur on this line

Bash, therefore needs to make use of some other tools - such as bc:

d=`echo "scale = 2;sqrt($c)" | bc`

echo $d # output will be 5.19

or awk:

d=`echo $c | awk '{print sqrt($1)}'`

echo $d #output will be 5.19615

However, if a Linux user wants to be able to simplify their calculations then they must turn to another shell - the Korn shell.

Using the Korn Shell

The Bourne-Again shell is set as the default in most (if not all) of the Linux distributions (Debian, Red Hat, Suse, etc, etc); however the Korn shell is normally installed - and it does everything that Bash does plus a little extra - such as built-in mathematics. It's therefore just a matter of telling the PC to use the correct shell; and this can be done from the command line by typing:

ksh

or in a shell script by setting the fist line to be:

#!/bin/ksh

Korn Shell Mathematical Functions

When using the Korn shell the following functions are available to the user:

  • abs - the absolute value
  • acos - the arccosine
  • asin - the arcsine
  • atan - the arctangent
  • atan2 - the arctangent of co-ordinates
  • cos - the cosine
  • cosh - the hyperbolic cosine
  • exp - the exponential (e to the power x)
  • fmod - the floating point remainder
  • hypot - the Euclidean distance
  • int - the integer
  • log - the natural log
  • pow - the exponentiation ( e.g. x to the power y)
  • sin - the sine
  • sinh - the hyperbolic sine
  • sqrt - the square root
  • tan - the tangent
  • tanh - the hyperbolic tangent

Examples of Using the Korn Shell Mathematical Functions

The most obvious example is the code at the start of this article, and so a complete Korn shell script to calculate the multiplication and square root would be:

#!/bin/ksh

(( a = 3 ))

(( b = 9 ))

(( c = a * b ))

(( d = sqrt(c) ))

echo $d

All of the functions have the same basic format:

(( result = function(input) ))

However, there are a few functions that may give unexpected results and those are the trigonometric and inverse trigonometric functions. The confusion can be caused by the fact that they expect to be working with radians rather than degrees. Of course, with that knowledge the Korn script writer will have no more problems:

(( pi = acos(-1) )) # A simple way of calculating pi

(( d = 30 )) #Start with 30 degrees

(( r = d * pi / 180 )) #Convert degrees to radians

(( c = cos(r) )) # Find cosine

(( r2 = acos(c) )) # Get arccosine

(( d2 = r2 * 180 / pi )) #Convert back to degrees

echo $d2 # Back to original number i.e. 30 degrees

Conclusion

For most Linux command line tasks or shell scripting the default Bourne-Again shell (Bash) does the job beautifully. However, when it's few limitations are encountered then the Korn shell is a great substitute and is a powerful addition to the shell programmer's set of tools.


The copyright of the article Mathmatical Functions and Linux Shells in Command Line Programming is owned by Mark Alexander Bain. Permission to republish Mathmatical Functions and Linux Shells in print or online must be granted by the author in writing.


The Korn shell has mathematical functions, Mark Alexander Bain
Bash is powerful, but does have its limitations, Mark Alexander Bain
The Korn shell extends Bash's functionility, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo