You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vidar Holen edited this page Dec 12, 2021
·
2 revisions
In POSIX sh, arithmetic for loops are undefined.
Problematic code:
for((i=0; i<10; i++))doecho"$i"done
Correct code:
i=0
while [ "$i"-lt 10 ]
doecho"$i"
i=$((i+1))done
Rationale:
C-style arithmetic for loops are a Ksh/Bash feature that's not supported by POSIX sh or dash. Use a while loop with separate initialization and incrementing instead.
Exceptions:
None
Related resources:
Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!