Arithmetic expressions

Arithmetic expressions are used in several situations:

These expressions are evaluated following some rules described below. The operators and rules of arithmetic expressions are mainly derived from the C programming language.

This article describes the theory of the used syntax and the behaviour. To get practical examples without big explanations see the article on Greg's wiki.

Constants

Mathematical constants are simply fixed values you write: 1, 3567 or 4326. Bash interprets some notations special:

  • 0... (leading zero) is interpreted as octal value
  • 0x... is interpreted as hex value
  • 0X... also interpreted as hex
  • <BASE>#... is interpreted as number according to the specified base <BASE>, e.g. 2#00111011

If you have a constant set in a variable, like:

x=03254
this is interpreted as octal value. If you want it to be interpreted as decimal value, you need to expand the parameter and specify base 10:
# this is interpreted decimal:
echo $(( 10#$x ))

# this is interpreted octal:
echo $(( x ))

# this is an invalid digit for base 10 (the "x")...:
echo $(( 10#x ))

Different bases

For a constant, the base can be specified using the form

<BASE>#<DIGITS...>

Regardless of the specified base, the arithmetic expressions will, if ever displayed, be displayed in decimal!

When no base is specified, the base 10 (decimal) is assumed, except the prefixes as mentioned above (octals, hexadecimals) are present. The specified base can go from 2 to 64. To represent digits in a specified base greater 10, more characters than 0 to 9 are needed (in that order, low ⇒ high):

  • 0 ... 9
  • a ... z
  • A ... Z
  • @
  • _

Let's quickly invent a new number system with base 43 to show what I mean:

$ echo $((43#1))
1

$ echo $((43#a))
10

$echo $((43#A))
36

$ echo $((43#G))
42

$ echo $((43#H))
bash: 43#H: value too great for base (error token is "43#H")

If you have no clue what a base is and why there might be other bases, and what numbers are and how they are built, then you don't need different bases.

If you want to convert between the usual bases (octal, decimal, hex), use the printf command and its format strings.

Shell variables

Shell variables can of course be used as operands, even when the integer attribute is not turned on (by declare -i <NAME>). If the variable is empty (null) or unset, its reference evaluates to 0. If the variable doesn't hold a value that looks like a valid expression (numbers or operations), the expression is re-used to reference for example the named parameters, e.g.:

test=string
string=3

echo $((test))
# will output "3"!
Of course, in the end, when it finally evaluates to something that is not a valid arithmetic expression (newlines, ordinary text, ...) then you'll get an error.

When variables are referenced, the notation 1 + $X is equivalent to the notation 1 + X, both are allowed.

When variables are referenced like $X, the rules of parameter expansion apply and are performed before the expression is evaluated. Thus, a construct like ${MYSTRING:4:3} is valid inside the arithmetic expression.

Truth

Unlike command exit and return codes, arithmetic expressions evaluate to logic “true” when they are not 0. When they are 0, they evaluate to “false”. The arithmetic evaluation compound command reverses the “truth” of an arithmetic expression to match the “truth” of command exit codes:

  • if the arithmetic expression brings up a value not 0, it returns 0
  • if the arithmetic expression evaluates to 0, it returns 1

That means, the following if-clause will execute the else-thread:

if ((0)); then
  echo "true"
else
  echo "false"
fi

Operators

Assignment

OperatorDescription
<ID> = <EXPR>normal assignment
<ID> *= <EXPR>equivalent to <ID> = <ID> * <EXPR>, see calculation operators
<ID> /= <EXPR>equivalent to <ID> = <ID> / <EXPR>, see calculation operators
<ID> %= <EXPR>equivalent to <ID> = <ID> % <EXPR>, see calculation operators
<ID> += <EXPR>equivalent to <ID> = <ID> + <EXPR>, see calculation operators
<ID> -= <EXPR>equivalent to <ID> = <ID> - <EXPR>, see calculation operators
<ID> <<= <NUMBER>equivalent to <ID> = <ID> << <NUMBER>, see bit operations
<ID> >>= <NUMBER>equivalent to <ID> = <ID> >> <NUMBER>, see bit operations
<ID> &= <EXPR>equivalent to <ID> = <ID> & <EXPR>, see bit operations
<ID> ^= <EXPR>equivalent to <ID> = <ID> ^ <EXPR>, see bit operations
<ID> |= <EXPR>equivalent to <ID> = <ID> | <EXPR>, see bit operations

Calculations

OperatorDescription
*multiplication
/division
%remainder (modulo)
+addition
-subtraction
**exponentiation

Comparisons

OperatorDescription
<comparison: less than
>comparison: greater than
<=comparison: less than or equal
>=comparison: greater than or equal
==equality
!=inequality

Bit operations

OperatorDescription
~bitwise negation
<<bitwise shifting (left)
>>bitwise shifting (right)
&bitwise AND
^bitwise exclusive OR (XOR)
|bitwise OR

Logical

OperatorDescription
!logical negation
&&logical AND
||logical OR

Misc

OperatorDescription
id++post-increment of the variable id (not required by POSIX®)
id--post-decrement of the variable id (not required by POSIX®)
++idpre-increment of the variable id (not required by POSIX®)
--idpre-decrement of the variable id (not required by POSIX®)
+unary plus
-unary minus
<EXPR> ? <EXPR> : <EXPR>conditional (ternary) operator
<EXPR> , <EXPR>expression list
( <EXPR> )subexpression (to force precedence)

Precedence

The operator precedence is as follows (highest → lowest):

  • Postfix (id++, id--)
  • Prefix (++id, --id)
  • Unary minus and plus (-, +)
  • Logical and bitwise negation (!, ~)
  • Exponentiation (**)
  • Multiplication, division, remainder (*, /, %)
  • Addition, subtraction (+, -)
  • Bitwise shifts (<<, >>)
  • Comparison (<, >, <=, >=)
  • (In-)equality (==, !=)
  • Bitwise AND (&)
  • Bitwise XOR (^)
  • Bitwise OR (|)
  • Logical AND (&&)
  • Logical OR (||)
  • Ternary operator (<EXPR> ? <EXPR> : <EXPR>)
  • Assignments (=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=)
  • Expression list operator (<EXPR> , <EXPR>)

The precedence can be adjusted using subexpressions of the form ( <EXPR> ) at any time. These subexpressions are always evaluated first.

Arithmetic expressions in Bash

syntax/arith_expr.txt · Last modified: 2009/09/25 07:39 by thebonsai
www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0