${(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 ist eine Zsh IDE mit Unterstützung für ${(A)…} - probieren Sie es jetzt aus!
© 2020–2024 Joachim Ansorg
Impressum
Datenschutz
Rechtliches