是否可以在 bash 中使用 $array=() ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10497425/
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
Is it possible to use $array=() in bash?
提问by Qben
Two things, first this is my first question in this forum and I do apologise if the formating is all over the place. Second I have not written that many bash scripts, and it tend to be quite a long time between the scripts I produce.
两件事,首先这是我在这个论坛上的第一个问题,如果格式化到处都是,我深表歉意。其次,我没有编写那么多 bash 脚本,而且我生成的脚本之间往往间隔很长时间。
That said, here is my question.
也就是说,这是我的问题。
Is it possible to do something like this in bash (Clear array $array contains):$array=()
是否可以在 bash 中做这样的事情(清除数组 $array 包含):$array=()
Basically this is what I would like to do. I have a variable with array variable names in it:
基本上这就是我想做的。我有一个带有数组变量名称的变量:
array1=()
array2=()
arrayList="array1 array2"
# In a function far far away
for array in $arrayList
do
eval arr=("\"${$array[@]\"")
for index in ${!arr[@]}
do
echo "${arr[$index]}"
done
# Here is the big "?", I like to clear the array that $array refers to.
$array=()
done
My arrays contain strings that include "" (space) and this is why I use the
eval
statement. Not sure it's needed but at least it's working. The script is more or less working as I want it too, but I need to clear the arrays in the $arrayList
, and I rather not hardcode it somewhere, even though that would be easy.
我的数组包含包含“ ”(空格)的字符串,这就是我使用该
eval
语句的原因。不确定它是否需要,但至少它正在工作。该脚本或多或少也像我想要的那样工作,但我需要清除$arrayList
.
回答by FatalError
Probably the simplest thing to do is just unset
them. An unset variable will act identically to an empty array in most contexts, and unset $array
ought to work fine.
可能最简单的事情就是unset
他们。在大多数情况下,未设置的变量与空数组的作用相同,并且unset $array
应该可以正常工作。
回答by Charles Duffy
You can't do $foo=bar
ever-- that isn't how indirect assignments in bash work. Unfortunately, while being able to do indirect array assignments is an available feature in ksh93, it isn't a formally documented available feature in bash.
你$foo=bar
永远做不到——这不是 bash 中的间接赋值的工作方式。不幸的是,虽然能够进行间接数组分配是 ksh93 中的一个可用功能,但它并不是 bash 中正式记录的可用功能。
Quoting BashFAQ #6(which should be read in full if you're interested in knowing more about using indirect variables in general):
引用BashFAQ #6(如果您有兴趣了解更多关于一般使用间接变量的信息,请阅读全文):
We are not aware of any trick that can duplicate that functionality in POSIX or Bourne shells (short of using eval, which is extremely difficult to do securely). Bash can almost do it -- some indirect array tricks work, and others do not, and we do not know whether the syntax involved will remain stable in future releases. So, consider this a use at your own risk hack.
# Bash -- trick #1. Seems to work in bash 2 and up. realarray=(...) ref=realarray; index=2 tmp="$ref[$index]" echo "${!tmp}" # gives array element [2] # Bash -- trick #2. Seems to work in bash 3 and up. # Does NOT work in bash 2.05b. tmp="$ref[@]" printf "<%s> " "${!tmp}"; echo # Iterate whole array.
我们不知道有任何技巧可以在 POSIX 或 Bourne shell 中复制该功能(缺少使用eval,这很难安全地做到)。Bash 几乎可以做到——一些间接数组技巧有效,而另一些则无效,我们不知道所涉及的语法在未来版本中是否会保持稳定。因此,请将此视为您自担风险的黑客使用。
# Bash -- trick #1. Seems to work in bash 2 and up. realarray=(...) ref=realarray; index=2 tmp="$ref[$index]" echo "${!tmp}" # gives array element [2] # Bash -- trick #2. Seems to work in bash 3 and up. # Does NOT work in bash 2.05b. tmp="$ref[@]" printf "<%s> " "${!tmp}"; echo # Iterate whole array.
However, clearing is simpler, as unset $array
will work fine.
但是,清除更简单,因为它unset $array
可以正常工作。
回答by SteinAir
array=()
It clears the array. I guess that is what you wanted..
它清除数组。我想这就是你想要的..