bash Shell 脚本:声明空数组的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18921350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:13:15  来源:igfitidea点击:

Shell Script: correct way to declare an empty array

arraysbashshell

提问by luizfzs

I'm trying to declare an empty array in Shell Script but I'm experiencing an error.

我正在尝试在 Shell 脚本中声明一个空数组,但遇到错误。

#!/bin/bash

list=$@

newlist=()

for l in $list; do

        newlist+=($l)

done

echo "new"
echo $newlist

When I execute it, I get test.sh: 5: test.sh: Syntax error: "(" unexpected

当我执行它时,我得到 test.sh: 5: test.sh: Syntax error: "(" unexpected

采纳答案by konsolebox

Run it with bash:

用 bash 运行它:

bash test.sh

And seeing the error, it seems you're actually running it with dash:

看到错误,您似乎实际上是在用破折号运行它:

> dash test.sh
test.sh: 5: test.sh: Syntax error: "(" unexpected

Only this time you probably used the link to it (/bin/sh -> /bin/dash).

只是这次您可能使用了它的链接(/bin/sh -> /bin/dash)。

回答by shaffooo

I find following syntax more readable.

我发现以下语法更具可读性。

declare -a <name of array>

For more details see Bash Guide for Beginners: 10.2. Array variables.

有关更多详细信息,请参阅Bash 初学者指南:10.2。数组变量

回答by TMT

In BASH 4+ you can use the following for declaring an empty Array:

在 BASH 4+ 中,您可以使用以下内容来声明一个空数组:

declare -a ARRAY_NAME=()

You can then append new items NEW_ITEM1 & NEW_ITEM2 by:

然后,您可以通过以下方式附加新项目 NEW_ITEM1 和 NEW_ITEM2:

ARRAY_NAME+=(NEW_ITEM1)
ARRAY_NAME+=(NEW_ITEM2)

Please note that parentheses () is required while adding the new items. This is required so that new items are appended as an Array element. If you did miss the (), NEW_ITEM2 will become a String append to first Array Element ARRAY_NAME[0].

请注意,添加新项目时需要括号 ()。这是必需的,以便将新项目作为 Array 元素附加。如果您确实错过了 (),NEW_ITEM2 将成为附加到第一个数组元素 ARRAY_NAME[0] 的字符串。

Above example will result into:

上面的例子将导致:

echo ${ARRAY_NAME[@]}
NEW_ITEM1 NEW_ITEM2

echo ${ARRAY_NAME[0]}
NEW_ITEM1

echo ${ARRAY_NAME[1]}
NEW_ITEM2

Next, if you performed (note the missing parenthesis):

接下来,如果您执行了(注意缺少的括号):

ARRAY_NAME+=NEW_ITEM3

This will result into:

这将导致:

echo ${ARRAY_NAME[@]}
NEW_ITEM1NEW_ITEM3 NEW_ITEM2

echo ${ARRAY_NAME[0]}
NEW_ITEM1NEW_ITEM3

echo ${ARRAY_NAME[1]}
NEW_ITEM2

Thanks to @LenW for correcting me on append operation.

感谢@LenW 纠正我的追加操作。

回答by Usul Muhadiv

Try this to see if you are oriented to dash or bash

试试这个,看看你是面向 dash 还是 bash

ls -al /bin/sh

If it says /bin/sh -> /bin/dash, then type this:

如果它说/bin/sh -> /bin/dash,然后输入:

sudo rm /bin/sh
sudo ln -s /bin/bash /bin/sh

Then type again:

然后再次输入:

ls -al /bin/sh*

then must says something like this:

那么必须这样说:

/bin/sh -> /bin/bash

It means that now shis properly oriented to Bash and your arrays will work.

这意味着现在sh正确地面向 Bash 并且您的数组将起作用。

回答by nouseforname

If the array is empty just do this:

如果数组为空,只需执行以下操作:

NEWLIST=

You can check it with:

您可以通过以下方式检查:

if [ $NEWLIST ] ; then
   # do something
fi

a non empty array declaration looks like this:

非空数组声明如下所示:

NEWLIST=('1' '2' '3')

To fill an array during process:

在过程中填充数组:

ARRAY=("$(find . -name '*.mp3')")

Hope this helps

希望这可以帮助