${(A)…}: Convert substitution into array expression

Converts the substitution of the value into an array expression, regardless of the name of the variable.

Field splitting, e.g. ${(A)=name}, is helpful for scalar values of name.

If AA, then the substitution is interpreted as an associative array.

Examples

Convert a list of key-value pairs into an associative array
1
2
3
4
5
6
key_value_list="a 1 b 2 c 3"
unset myArray
# 1st '=' splits key_value_list into separate words, 
# 2nd '=' declares and assigns to associative array 'myArray' 
: ${(AA)=myArray=$key_value_list}
typeset -p myArray
Output:
1
typeset -A myArray=( [a]=1 [b]=2 [c]=3 )
BashSupport Pro 是一款支持 A** 的 **Zsh 集成开发环境 - 立即试用
© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款