I want to use the values of one array to point to values in another, but I can't figure it out. I think this is very basic. I'd like the file to yield one line associated with each of x, y, z as follows: x XValue, y Yvalue, Z Zvalue but I just get the first line.

#! /bin/bash
declare -a id
declare -A header

id=( x y z )

header[x]="X Value"
header[y]="Y Value"
header[z]="Z Value"

for ident in $id
    echo $ident ${header[${ident}]}
  • 3
    Change $id to "${id[@]}".
    – cuonglm
    Dec 8, 2015 at 2:30
  • Non-numeric indices only work in bash 4 or greater.
    – DopeGhoti
    Dec 8, 2015 at 3:07

1 Answer 1


You need to use ${id[@]} to indicate all elements of array id:

$ for i in "${id[@]}"; do echo "This is "${header["$i"]}""; done
This is X Value
This is Y Value
This is Z Value

While you are getting only the first element using $id:

$ for i in "$id"; do echo "This is "${header["$i"]}""; done
This is X Value

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .