Dec 312011

Sometimes in my small shell script i’ve to do some math, usually nothing too complex but it’s useful to do the math inside the bash script. Bash it’s really complete and among the many builtin functions there are also some mathematical functions.

Using the syntax $((expression)) you can evaluate the expression. Operations allowed are quite a few. From  man bash:

The shell allows arithmetic expressions to be evaluated, under certain circumstances. Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, asso ciativity, and values are the same as in the C language.

The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.

       id++ id--   variable post-increment and post-decrement
       ++id --id   variable pre-increment and pre-decrement
       - +             unary minus and plus
       ! ~             logical and bitwise negation
       **              exponentiation
       * / %         multiplication, division, remainder
       + -             addition, subtraction
       << >>       left and right bitwise shifts
       <= >= < >       comparison
       == !=       equality and inequality
       &               bitwise AND
       ^               bitwise exclusive OR
       |                bitwise OR
       &&             logical AND
       ||              logical OR
       expr?expr:expr   conditional operator
       = *= /= %= += -= <>= &= ^= |=                        assignment
       expr1 , expr2                        comma

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is ref erenced, or when a variable which has been given the integer attribute using declare -i is assigned a value. A null value evaluates to 0. A shell variable need not have its integer attribute turned on to be used in an expression.

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Other wise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arith metic base, and n is a number in that base. If base# is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.

Enhanced by Zemanta

Popular Posts:

flattr this!

  2 Responses to “Doing calculations using bash”

  1. Ah – I didn’t know you could do that! The fact that you can’t seem to evaluate non-integer numbers seems to be a bit of a limitation.
    And I don’t like the way you get an error message returned with every calculation, although that’s more of an aesthetic thing.
    I normally use the Python interactive prompt for quick mathematical operations.

 Leave a Reply




You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>