How to access further members of an array when using bash variable indirection?
2
votes
1
answer
109
views
Consider the following example, it seems it's working fine with the index
0
:
$ a1=(1 2 3)
$ a2=(a b c)
$ for x in a1 a2; do echo "${!x}"; done
1
a
$ for x in a1 a2; do echo "${!x}"; done
1
a
However with the index 1
it prints nothing:
$ for x in a1 a2; do echo "${!x}"; done
Arrays just by themselves are fine:
$ echo "${a1} ${a2}"
2 b
### Edit - A real life use case based on *ilkkachu* answer
SHIBB=(https://shibboleth.net/downloads/service-provider/3.0.2/ shibboleth-sp-3.0.2 .tar.gz)
XERCES=(http://apache.mirrors.nublue.co.uk//xerces/c/3/sources/ xerces-c-3.2.1 .tar.gz)
XMLSEC=(http://apache.mirror.anlx.net/santuario/c-library/ xml-security-c-2.0.1 .tar.gz)
XMLTOOL=(http://shibboleth.net/downloads/c++-opensaml/latest/ xmltooling-3.0.2 .tar.gz)
OPENSAML=(http://shibboleth.net/downloads/c++-opensaml/latest/ opensaml-3.0.0 .tar.gz)
typeset -n x
for x in XERCES XMLSEC XMLTOOL OPENSAML SHIBB; do
url="${x}" app="${x}" ext="${x}"
[ -f "./${app}${ext}" ] || wget "${url}${app}${ext}"
tar -xf "./${app}${ext}"
cd "./${app}" && ./configure && make -j2 && make install && ldconfig
cd ..
done
Asked by NarūnasK
(2525 rep)
Sep 4, 2018, 08:03 PM
Last activity: Oct 15, 2018, 08:03 PM
Last activity: Oct 15, 2018, 08:03 PM