* Assignment statements, and arguments to declaration commands of variables with the integer attribute.
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 [[http://mywiki.wooledge.org/BashGuide/CompoundCommands#Arithmetic_Evaluation | this page on Greg's wiki]].
===== Constants =====
Mathematical constants are simply fixed values you write: ''1'', ''3567'', or ''4326''. Bash interprets some notations specially:
* ''0...'' (leading zero) is interpreted as an **octal** value
When no base is specified, the base 10 (decimal) is assumed, except when the prefixes as mentioned above (octals, hexadecimals) are present. The specified base can range from 2 to 64. To represent digits in a specified base greater than 10, characters other than 0 to 9 are needed (in this order, low => high):
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 [[commands:builtin:printf | the printf command]] and its format strings.
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.:
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 [[syntax:pe | parameter expansion]] apply and are performed **before** the expression is evaluated. Thus, a construct like ''${MYSTRING:4:3}'' is valid inside an arithmetic expression.
===== Truth =====
Unlike command exit and return codes, arithmetic expressions evaluate to logical "true" when they are not 0. When they are 0, they evaluate to "false". The [[syntax:ccmd:arithmetic_eval | 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 (arithmetic true), it returns 0 (shell true)
* if the arithmetic expression evaluates to 0 (arithmetic false), it returns 1 (shell false)
That means, the following ''if''-clause will execute the ''else''-thread:
|''<ID> *= <EXPR>''|equivalent to ''<ID> = <ID> * <EXPR>'', see [[syntax:arith_expr#calculations | calculation operators]]|
|''<ID> /= <EXPR>''|equivalent to ''<ID> = <ID> / <EXPR>'', see [[syntax:arith_expr#calculations | calculation operators]]|
|''<ID> %= <EXPR>''|equivalent to ''<ID> = <ID> % <EXPR>'', see [[syntax:arith_expr#calculations | calculation operators]]|
|''<ID> += <EXPR>''|equivalent to ''<ID> = <ID> + <EXPR>'', see [[syntax:arith_expr#calculations | calculation operators]]|
|''<ID> -= <EXPR>''|equivalent to ''<ID> = <ID> - <EXPR>'', see [[syntax:arith_expr#calculations | calculation operators]]|
|''<ID> <nowiki><<=</nowiki> <NUMBER>''|equivalent to ''<ID> = <ID> <nowiki><<</nowiki> <NUMBER>'', see [[syntax:arith_expr#bit_operations | bit operations]]|
|''<ID> <nowiki>>>=</nowiki> <NUMBER>''|equivalent to ''<ID> = <ID> <nowiki>>></nowiki> <NUMBER>'', see [[syntax:arith_expr#bit_operations | bit operations]]|
|''<ID> &= <EXPR>''|equivalent to ''<ID> = <ID> & <EXPR>'', see [[syntax:arith_expr#bit_operations | bit operations]]|
|''<ID> ^= <EXPR>''|equivalent to ''<ID> = <ID> ^ <EXPR>'', see [[syntax:arith_expr#bit_operations | bit operations]]|
|''<ID> <nowiki>|=</nowiki> <EXPR>''|equivalent to ''<ID> = <ID> <nowiki>|</nowiki> <EXPR>'', see [[syntax:arith_expr#bit_operations | bit operations]]|
===== Arithmetic expressions and return codes =====
Bash's overall language construct is based on exit codes or return codes of commands or functions to be executed. ''if'' statements, ''while'' loops, etc., they all take the return codes of commands as conditions.
Now the problem is: The return codes (0 means "TRUE" or "SUCCESS", not 0 means "FALSE" or "FAILURE") don't correspond to the meaning of the result of an arithmetic expression (0 means "FALSE", not 0 means "TRUE").
That's why all commands and keywords that do arithmetic operations attempt to **translate** the arithmetical meaning into an equivalent return code. This simply means:
* if the arithmetic operation evaluates to 0 ("FALSE"), the return code is not 0 ("FAILURE")
* if the arithmetic operation evaluates to 1 ("TRUE"), the return code is 0 ("SUCCESS")
This way, you can easily use arithmetic expressions (along with the commands or keywords that operate them) as conditions for ''if'', ''while'' and all the others, including ''set -e'' for autoexit on error: