WesZ
WesZ

Reputation: 139

Bash printing value of array reference

p=0
array="host_01"
HostProjects[$p]="project_01"
AvgCr=1569.22
eval "${array}_${HostProjects[$p]}=$AvgCr"
echo "Host Credit is ${host_01_project_01}"

Gives me 1569.22

But how can I get the result 1569.22 from:

printf '%s\n' "${array}_${HostProjects[$p]}"
or even from:
echo "${array}_${HostProjects[$p]}"

Which gives me host_01_project_01

I have tried several things but it ends up in syntax errors. Thanks.

Upvotes: 0

Views: 27

Answers (1)

iBug
iBug

Reputation: 37237

If you're using Bash, indirect parameter expansion is your friend:

varname="${array}_${HostProjects[$p]}"
echo "The value you want is ${!varname}"

Upvotes: 1

Related Questions