2

I want to initialize an array in sh.

In bash that would be:

list=(`seq 1 4`)

In sh I try to do it like this:

    for i in `seq 1 4`; do
        list[$((i-1))]="$i"
    done

I get an error though for each iteration saying:

list[0]=1: not found

What am I doing wrong and how to fix that?

1

2 Answers 2

4

POSIX sh doesn't support arrays. You need a more advanced shell for that, e.g. bash, zsh, or ksh.

Sign up to request clarification or add additional context in comments.

3 Comments

Not really. It just fails to mention that only some shells support it.
sh on OSX seems to support arrays.
Only because sh on OS X is really bash, which is a bit loose about what it accepts in "POSIX" mode.
1

If you really want to use arrays, you can fudge them by writing your own array function. I'm not going to encourage this by giving you a full function :-) but here's the gist:

$ f0=yay 
$ t=0
$ eval echo f$t
f0
$ eval echo \$f$t
yay

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.