June 2017
Beginner
502 pages
11h 26m
English
This is the unary operator ++ (or --), which allows us to increase/decrease the value of a variable of 1 and reassign it, but be careful, the position of the operator matters:
zarrelli:~$ a=15 ; echo $((a++)) ; echo ${a}1516zarrelli:~$ a=15 ; echo $((++a)) ; echo ${a}1616
Can you figure out what happened? Simply, in the first case, the first operator returned the value and, only after, added 1 to it; in the second case, first it added 1 to the value and then returned it. Pay attention to this operator because it is widely used inside loops to count the cycles and eventually break out from them:
zarrelli:~$ cat loop.sh #!/bin/bash counter=10 while [ $counter -gt 0 ]; doecho"Loop number: $((counter--))"done
We instance ...
Read now
Unlock full access