Differences between revisions 2 and 3
Revision 2 as of 2023-01-21 06:08:57
Size: 3195
Comment:
Revision 3 as of 2023-01-21 21:59:50
Size: 3763
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

`bash(1)` is capable of doing integer arithmetic.
Line 9: Line 11:
== Numeric Literals == == Integer Literals ==
Line 19: Line 21:
To force evaluation as a ''positive'' base 10 integer, try:

{{{
a=$(( 10#$a ))
}}}

Note that this will not work if `$a` is signed (i.e. `+1`, `-1`).
Line 22: Line 32:

== Usage ==

`bash(1)` parses tokens as arithmetic in any of the following contexts:

 * the `let` builtin
 * `$(( ... ))`
 * [[Bash/Expansion#Arithmetic_Expansion|Arithmetic expansions]] (i.e. `(( ... ))`)
 * [[Bash/Looping#For_Loops|C-style for loops]] (i.e. `for ((i=0; i<10; i++)); do ... done`)
 * [[Bash/Array|Array indexing]]

----
Line 55: Line 77:
||`==`        ||equality || ||`=` or `==` ||equality ||
Line 88: Line 110:
Note that ternary operators follow [[Bash/Logic#Conditional_Commands|logic operators]] as well. Note that assignment operators follow [[Bash/Logic#Conditional_Commands|logic operators]] as well.

Bash Arithmetic

bash(1) is capable of doing integer arithmetic.


Integer Literals

Literals follow the C language definition.

A leading 0 indicates an octal number.

A leading 0x or 0X indicates hexadecimal.

Otherwise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arithmetic base. If base# is omitted, the default of base 10 applies. When specifying n, if a non-digit is required, the digits greater than 9 are represented by lowercase letters, uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters are interchange.

To force evaluation as a positive base 10 integer, try:

a=$(( 10#$a ))

Note that this will not work if $a is signed (i.e. +1, -1).


Usage

bash(1) parses tokens as arithmetic in any of the following contexts:


Unary Operators

Unary operators have the highest priority.

Operator

Meaning

a++

post-increment $a

a--

post-decrement $a

++a

pre-increment $a

--a

pre-decrement $a

+

positive

-

negative

!

negation


Binary Operators

The binary operators include arithmetic and logic operations.

Operator

Meaning

*

multiplication

/

division

%

remainder

+

addition

-

subtraction

**

exponentiation

<

less than

>

greater than

<=

less than or equal

>=

greater than or equal

= or ==

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 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 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

Bash/Arithmetic (last edited 2023-01-29 21:14:38 by DominicRicottone)