我的主目录.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中的路径名扩展如何发挥作用?
答案 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