mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC3005 (markdown)
33
SC3005.md
Normal file
33
SC3005.md
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
## In POSIX sh, arithmetic for loops are undefined.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
for ((i=0; i<10; i++))
|
||||||
|
do
|
||||||
|
echo "$i"
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
i=0
|
||||||
|
while [ "$i" -lt 10 ]
|
||||||
|
do
|
||||||
|
echo "$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!
|
Reference in New Issue
Block a user