initializing arrays in sh

旧时模样 提交于 2019-12-20 02:58:09

问题


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:


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




回答2:


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


来源:https://stackoverflow.com/questions/20455819/initializing-arrays-in-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!