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 examples 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:
150
And now another one, please:
50
The user_input variable value is: 150
The adding variable value is: 50
150 added of 50 is: 200
And the user_input variable has now the value of 200
But the adding variable has still the value of 50

Easy! And we did not have to reassign the value of 200 explicitly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.230.107