Bash数组和路径名扩展:奇怪的输出

时间:2013-08-30 16:36:57

标签: arrays bash pathname

我的主目录.phpsh.php_history中有两个文件并使用它们的名称我想测试路径名的扩展以及它如何影响bash中的关联数组。我遇到的结果似乎很奇怪,我无法解释它们。我在这里试过了很多变化。

 >my_array_filename_var=(".phpsh"=10 ".php_history" =20)
 >echo ${my_array_filename_var[.phpsh]}
 ERROR
 >echo "${my_array_filename_var[.phpsh]}"
 ERROR
 # This one below understandably doesn't work. But I was pulling my hair out.
 >echo "${my_array_filename_var[".phpsh"]}"
 ERROR
 echo "${my_array_filename_var[\".phpsh\"]}"
 ERROR
 >echo "${my_array_filename_var[.phpsh]}"
 ERROR
 >echo "${my_array_filename_var[phpsh]}"
 .phpsh=10
 >echo "${my_array_filename_var[php_history]}"
 .phpsh=10
 >echo "${my_array_filename_var[\.php_history]}"
 ERROR

ERROR代币代表

bash: .phpsh: syntax error: operand expected (error token is ".phpsh")

是否有一些规则说我的关键数组的键中没有点字符? bash中的路径名扩展如何发挥作用?

1 个答案:

答案 0 :(得分:5)

你应该{b}中declare -A(大写A)一个关联数组。这是正确的语法:

$ declare -A my_array_filename_var=([.phpsh]=10 [.php_history]=20)
$ echo "${my_array_filename_var[.phpsh]}"
10

$ declare -A my_array_filename_var
$ my_array_filename_var=([.phpsh]=10 [.php_history]=20)
$ echo "${my_array_filename_var[.phpsh]}"
10