Assignment operators
We have seen how to manipulate the value to a variable and an integer so far, and then reassign this value to another variable or the same one. But why use two operations when you can alter the value of a and reassign it at the same time using the assignment operators?
The += operator
This operator adds a quantity to the value of the variable and assigns the outcome to the variable itself, but to clarify its use, let's rewrite one of the we've seen before:
#!/bin/bash echo "Hello user, please give me a number: " read user_input echo "And now another one, please: "
Adding
echo "The user_input variable value is: ${user_input}" echo "The adding variable value is: ${adding}" echo "${user_input} added of ${adding} is: $((user_input+=adding))" echo "And the user_input variable has now the value of ${user_input}" echo"But the adding variable has still the value of ${adding}"
And now, let's run it, as follows:
zarrelli:~$ ./userreassign.sh Hello user, please give me a number...