Sage-Code Laboratory
index<--

Arithmetics Expressions

Bash is not a math genius. However if you must make some calculations in Bash you have all the ingredients. You can evaluate an expression and create a result. However Bash do not have float numbers only integers.
Example
#!/bin/bash
i=0

#expression as command
((i=i+1))

#expression value capture
i=$((i+1))

#usually we do it like this
let "i=i+1"

#this also works
let i=i+1

Notes: In this example there are 4 different ways to create and execute a simple expression. The question is which one is most convenient. Well when I'm finding the answer I will let you know. Until then I will just use them alternatively in different places of the code. Maybe you can contribute later making a comment.

Arithmetic Operators

Operator Description Example Result
+ Add two operands ((1+3)) 4
- Subtraction ((4-1)) 3
* Multiply two numbers ((4*4)) 16
/ Integer division ((4/3)) 1
% Find remainder of division ((10%3)) 1
++ Increment i=0; ((i++)) 1
-- Decrement i=1; ((i--)) 0
+= Addition modifier i=0; ((i+=2)) 2
-= Subtraction modifier i=4; ((i-=2)) 2
*= Multiplication modifier i=4; ((i*=4)) 16
** Exponent ((i=3**2)) 9
**= Exponent modifier i=3; ((i**=2)) 9

Low level computing

Believe it or not, Bash enable binary number, octal number and hexadecimal numbers. Actually it has a cool literal for these kind the numbers. Use notation: base#digits where base can be: 2,8,16 and digits can be (0,1) or (0..7) or (0..9ABCDEF).

Console
>bash
$ a=2#001
$ b=2#010    
$ echo $((a|b))
3
$

As you can see in above example I have used bash CLI to make a simple bitwise operation between two binary number. However, the bitwise operations work with decimal numbers as well. Binary representation or hex representation are good alternative but difficult to print out. Bash do not have direct format for printing out binary numbers.

Bitwise operators

Next operators are acting at bit level. They are inherited from C language. An operator usually has 2 operands but one of these operators has only one operand. Which one? Read description for each operator to find out!

Operator Name Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands ((a&b))
| Binary OR It copies a bit if it exists in either operand. ((a|b))
^ Binary XOR It copies the bit if it is set in one operand but not both. ((a^b))
~ Binary Ones Complement It is unary and has the effect of 'flipping' bits (not). ((~a))
<< Binary Left Shift The left operands value is moved left by the number of bits specified by the right operand. ((a<<2))
>> Binary Right Shift The left operands value is moved right by the number of bits specified by the right operand. ((a>>2))

Binary conversion

To print binary I have found on the internet next expression, and I have try to explain it here. It has take me an entire day to understend what's going on. Maybe you will find this useles, but is a good exercise of your future bash skills.

Example
#!/bin/bash
read -p "number:" n
echo $(echo "obase=2;$n" | bc)

Don't panic. I have created a script that can do the conversion, you can study this script much later after you learn the entire Bash syntax. For now is good to know that the expression above is using an external command called "bc" that is a "bynary calculator". This has it's own language. obase=2 is an option of this language to print a number in binary. This is called "comman expansion" and "pipeline". We we will learn how to make these kind of expressions much later. (LOL)

Homework: Open the next script. I have used native Bash operators to make a convertor. This will entice to to learn some more Bash, until you can fully understand this script. Don't be descouraged keep going and trust your teacher. I will explain step by step everything. Come back here after you finish the class and play with this script.

GitHub example:tobin.sh


Read next: Brace Expansion