= Shell Arithmetic =

`sh(1)` is capable of doing some simple forms of integer arithmetic.

<<TableOfContents>>

----



== Integer Literals ==

Literals follow the C language definition.

A leading `0` indicates an octal number.

A leading `0x` or `0X` indicates hexadecimal.

----



== Usage ==

An arithmetic context looks like:

{{{
a = $(( $a + 1 ))
}}}

----



== Unary Operators ==

Unary operators have the highest priority.

||'''Operator'''||'''Meaning'''      ||
||`+`           ||positive           ||
||`-` 	        ||negative           ||
||`!`           ||negation           ||

----



== Binary Operators ==

The binary operators include arithmetic and logic operations.

||'''Operator'''||'''Meaning'''        ||
||`*`           ||multiplication       ||
||`/`           ||division             ||
||`%`           ||remainder            ||
||`+`           ||addition             ||
||`-`           ||subtraction          ||
||`<`           ||less than            ||
||`>`           ||greater than         ||
||`<=`          ||less than or equal   ||
||`>=`          ||greater than or equal||
||`=`           ||equality             ||
||`!=`          ||inequality           ||

----



== Ternary Operators ==

There is just one ternary operator:

{{{
a ? b : c
}}}

`a` is the test, `b` is the value if true, and `c` is the value if false.

Note that ternary operators follow [[Shell/Logic#Conditional_Commands|logic operators]] as well.
 
----



== Assignment ==

||'''Operator'''||'''Meaning''' ||
||`a = b`       ||assignment    ||
||`a *= b`      ||`a = a * b`   ||
||`a /= b`      ||`a = a / b`   ||
||`a %= b`      ||`a = a % b`   ||
||`a += b`      ||`a = a + b`   ||
||`a -= b`      ||`a = a - b`   ||

Note that assignment operators follow [[Shell/Logic#Conditional_Commands|logic operators]] as well.

----



== Bit-wise Operators ==

||'''Operator'''||'''Meaning''' ||
||`~`           ||negation      ||
||`<<`          ||bitshift left ||
||`>>`          ||bitshift right||
||`&`           ||AND           ||
||`^`           ||XOR           ||
||`|`           ||OR            ||
||`a <<= b`     ||`a = a << b`  ||
||`a >>= b`     ||`a = a >> b`  ||
||`a &= b`      ||`a = a & b`   ||
||`a ^= b`      ||`a = a ^ b`   ||
||`a |= b`      ||`a = a | b`   ||

Note that negation has equal precedence to other unary operators; bitshifts (such as `<<`) follow arithmetic operators; bit-wise logic (such as `&`) come after all other binary operators; and bit-wise assignment operators (such as `<<=`) have equal precedence to other assignment operators.



----
CategoryRicottone