Prerequisites :
- Bash Version = 4.2.46
I wrote the following code:
declare -A service1=(
[id]=1
[name]=foo
)
declare -A service2=(
[id]=2
[name]=bar
)
declare -n service
for service in ${!service@}; do
echo ${service[id]} ${service[name]}
done
My expected output:
1 foo
2 bar
But I got errors when run with bash 4:
./test.sh: line 13: declare: -n: invalid option
declare: usage: declare [-aAfFgilrtux] [-p] [name[=value] ...]
service1 service1
service2 service2
I find declare -n is new for Bash(5):
w. The shell has
nameref' variables and new -n(/+n) options to declare and unset to use them, and atest -R' option to test for them.
How can I implement the above code in bash 4, other alternatives?