Skip to content

Instantly share code, notes, and snippets.

@YakDriver
Last active February 9, 2018 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YakDriver/aa4a5758b41d39491fe9c5b2fd2f1d9a to your computer and use it in GitHub Desktop.
Save YakDriver/aa4a5758b41d39491fe9c5b2fd2f1d9a to your computer and use it in GitHub Desktop.
Bash Basics: Increment variables
#!/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