Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 我可以使用多少个参数_Arrays_Shell_Parameters - Fatal编程技术网

Arrays 我可以使用多少个参数

Arrays 我可以使用多少个参数,arrays,shell,parameters,Arrays,Shell,Parameters,我有一个问题:跑步的时候 ./choose_words.sh $NWORDS_s1 $NWORDS_s2 $NWORDS_s3 $NWORDS_s4 在choose_words中,在执行nwords=($1$2$3$4)之后,$4似乎不包含任何值。因此,如果尝试打印: echo ${nwords[4]} # I get nothing from this 如果我试图打印echo${nwords[*]},数组nwords实际上有第四个元素及其实际值 这对您有意义吗?数组索引从0开始,因此需要使

我有一个问题:跑步的时候

./choose_words.sh $NWORDS_s1 $NWORDS_s2 $NWORDS_s3 $NWORDS_s4
在choose_words中,在执行
nwords=($1$2$3$4)
之后,
$4
似乎不包含任何值。因此,如果尝试打印:

echo ${nwords[4]} # I get nothing from this
如果我试图打印
echo${nwords[*]}
,数组
nwords
实际上有第四个元素及其实际值


这对您有意义吗?

数组索引从
0开始,因此需要使用
${nwords[3]}
获取数组的第四个元素。

数组索引从0开始,而不是从1;)

i、 e:


在什么壳里?通常,shell不支持数组,因此您必须谈论特定的shell。请确保引用传递给脚本的参数,以保护带有空格的参数。类似地,当您创建数组时:
nwords=(“$1”“$2”“$3”“$4”)
或更简单的
nwords=(“$@”)
echo ${nwords[0]} # This is the 1st element, corresponding to $1
echo ${nwords[1]} # This is the 2nd element, corresponding to $2
echo ${nwords[2]} # This is the 3rd element, corresponding to $3
echo ${nwords[3]} # This is the 4th element, corresponding to $4