Arithmetic operators
Arithmetic operators do what you think they do, that is, add, subtract, divide, and so on. It is something we are familiar with even without specific knowledge. Let's see each of them and how they can be used to manipulate the value of variables.
Before proceeding, keep in mind that for a shell script, a is a decimal unless you prefix that with a 0
for the octal, a 0x
for a hexadecimal number, or a base#number
for a number that evaluates on the base.
The + operator
This is like what we see at primary school; this operator us to add an integer to the value of the variable, as we can see in the following example:
#!/bin/bash echo "Hello user, please give me a number: " read user_input echo "And now another one, please: " read adding addition=$((user_input+adding)) echo "The number is: ${user_input:-99}" echo "The number added of ${adding} is: ${addition}"
Now time to invoke the script:
zarrelli:$ ./useraddition.sh Hello user, please give me a number: 120 And now another...