Skip to content

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

Description

@gregory-nisbet

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions