crocefisso
crocefisso

Reputation: 903

POSIX sh syntax for for-loops [SC2039]

The following command works well on my shell :

for ((j=0; j<=24; j++))
do > $j.json
done

but having the following notification :

SC2039 In POSIX sh, arithmetic for loops are undefined.

I was wondering what would be the equivalent in POSIX in order not to have interpeting issues on other systems.

Upvotes: 14

Views: 15565

Answers (2)

Johannes Weiss
Johannes Weiss

Reputation: 54081

Another way that should work in a POSIX shell:

for j in $(seq 0 24); do
    ...
done

Upvotes: 8

Tom Fenech
Tom Fenech

Reputation: 74685

I guess the standards-compliant way of doing it would be something like this:

j=0
while [ $j -le 24 ]; do
    true > "$j.json"
    j=$(( j + 1 ))
done

Upvotes: 26

Related Questions