I have been looking for a solution on this online but could not get much information (most multi-array examples are not being created from text docs) so here it goes. Say I have a script that reads in values from a text doc example.txt:

Tag1      Value1
Tag2      Value2
Tag3      Value3

Is there such a way where I can programmatically store Tag and Value into a 2-dimensional array so that it looks like:


(excuse the syntax please). I would want this done preferably in bash rather than awk.


bash does not do multi-dimensional arrays. Are you looking for an associative array?

declare -A arrayResult

while IFS=$'\t' read -r key value; do
done < example.txt

for key in "${!arrayResult[@]}"; do
    printf "%s\t%s\n" "$key" "${arrayResult["$key"]}"

Requires bash version 4 for associative arrays.

| improve this answer | |
  • Yes I am. I am doing it so that the program I am calling in my script which takes two parameters (one is for a tag, the other is for a value). – ryekayo Mar 4 '15 at 21:03
  • Note that it won't work for empty keys. zsh and ksh93 can work around that by using IFS=$'\t\t', not bash. – Stéphane Chazelas Mar 4 '15 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.