Sample Header Ad - 728x90

BASH associative array printing

41 votes
6 answers
59092 views
Is there a way to print an entire array ([key]=value) without looping over all elements? Assume I have created an array with some elements: declare -A array array=([a1]=1 [a2]=2 ... [b1]=bbb ... [f500]=abcdef) I can print back the entire array with for i in "${!array[@]}" do echo "${i}=${array[$i]}" done However, it seems bash already knows how to get all array elements in one "go" - both keys ${!array[@]} and values ${array[@]}. Is there a way to make bash print this info without the loop? Edit: typeset -p array does that! However I can't remove both prefix and suffix in a single substitution: a="$(typeset -p array)" b="${a##*(}" c="${b%% )*}" Is there a cleaner way to get/print only the key=value portion of the output?
Asked by Dani_l (5157 rep)
May 22, 2017, 04:13 PM
Last activity: Jan 5, 2025, 05:02 PM