6

in general i will use expr inside shell scripts for doing arithmetic operations.

is there a way where we can come up with arithmetic operation in a shell script without using expr?

3
  • I suppose "Use bc instead" isn't quite the answer you're looking for... Commented Jun 10, 2010 at 5:23
  • @ignacio...could you pleasegive an example of how to use bc? Commented Jun 10, 2010 at 5:25
  • PI=$(echo "4*a(1)" | bc -l) Commented Jun 10, 2010 at 13:57

3 Answers 3

5

Modern shells (POSIX compliant = modern in my view) support arithmetic operations: + - / * on signed long integer variables +/- 2147483647.

Use awk for double precision, 15 siginificant digits It also does sqrt.

Use bc -l for extended precision up to 20 significant digits.

The syntax (zed_0xff) for shell you already saw:

a=$(( 13 * 2 ))
a=$(( $2 / 2 ))
b=$(( $a - 1 ))
a=(( $a + $b ))

awk does double precision - floating point - arithmetic operations natively. It also has sqrt, cos, sin .... see:

http://people.cs.uu.nl/piet/docs/nawk/nawk_toc.html

bc has some defined functions and extended presision which are available with the -l option:

bc -l

example:

echo 'sqrt(.977)' | bc -l
Sign up to request clarification or add additional context in comments.

Comments

4

Did you tried to read "man ksh" if you're using ksh?

"man bash", for example, has enough information on doing arithmetics with bash.

the command typeset -i can be used to specify that a variable must be treated as an integer, for example typeset -i MYVAR specifies that the variable MYVAR is an integer rather than a string. Following the typeset command, attempts to assign a non integer value to the variable will fail:

   $ typeset -i MYVAR
   $ MYVAR=56
   $ echo $MYVAR
   56
   $ MYVAR=fred
   ksh: fred: bad number
   $

To carry out arithmetic operations on variables or within a shell script, use the let command. let evaluates its arguments as simple arithmetic expressions. For example:

   $ let ans=$MYVAR+45
   echo $ans
   101
   $

The expression above could also be written as follows:

   $ echo $(($MYVAR+45))
   101
   $

Anything enclosed within $(( and )) is interpreted by the Korn shell as being an arithmetic expression

2 Comments

In all fairness, the ksh man page is, um, not so helpful. The ksh88 man pages are even misleading, for example, explaining the _ variable, they tell you it relates to MAILPATH, but do not clearly explain what it is. (on solaris 9, HPUX 11.0 & 11i) It is the argument(s) to the previously entered command, if it matters.
0

The precision and range of shell builtin $(( <expr> )) depends on the platform. On a 32-bit platform it's limited to 32-bit integers (unlike expr, which appears to support larger integers in a way that appears to be independent of platform).

(N.B - beware that while ^ is a valid operator in most shells, it does not mean 'to the power of', but rather XOR.)

Also - some shells are more forthcoming in this regard. Zsh, for example, allows floating point operations, such as $(( 10/3.0 ))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.