Last active
February 9, 2018 19:47
-
-
Save YakDriver/aa4a5758b41d39491fe9c5b2fd2f1d9a to your computer and use it in GitHub Desktop.
Bash Basics: Increment variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 3 correct and 1 incorrect way to increment bash variables using "let" and arithmetic expansion. | |
# | |
# OUTPUT: | |
# | |
# 0 | |
# 0+1 | |
# 1 | |
# 2 | |
# 3 | |
# 4 | |
# 5 | |
# 6 | |
# 7 | |
# | |
num=0 ; echo $num | |
# 0 - FAIL | |
num=$num+1 ; echo $num ; num=0 | |
# 1 Reassign | |
let "num=num+1" ; echo $num | |
((num=num+1)) ; echo $num | |
num=$((num+1)) ; echo $num | |
# 2 += operator | |
let "num+=1" ; echo $num | |
((num+=1)) ; echo $num | |
# 3 ++ operator | |
let "num++" ; echo $num | |
((num++)) ; echo $num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment