init-script-template icon indicating copy to clipboard operation
init-script-template copied to clipboard

Brace expansion {1..10} in for loop is not portable

Open gregory-nisbet opened this issue 8 years ago • 0 comments

Bash, even in posix emulation mode, will expand braced sequences (I don't think that's the official name for this construction).

$ bash --posix -c 'echo {1..10}'
1 2 3 4 5 6 7 8 9 10

But a more minimal shell like dash will not perform this expansion

$ dash -c 'echo {1..10}'
{1..10}

seq is pretty common on Linux, but is not itself POSIX. It isn't available on FreeBSD, for instance.

for i in `seq 1 10`; do
    echo "$i"
done

The POSIXest way I can think of to do this is to use a while loop and an explicit test.

i=1
while [ "$i" -le 10 ]; do
    echo "$i"
    i="$((i+1))"
done

gregory-nisbet avatar Apr 21 '16 01:04 gregory-nisbet