0

I'm learning how bash scripting and I need to know how to get a value from an array of dictionaries. I did this for the declaration:

declare -a persons
declare -A person
person[name]="Bob"
person[id]=12
persons[0]=$person

If I do the following it works fine:

echo ${person[name]}
# Bob

But when I try to access to the values from the array it doesn't work. I tried these options:

echo ${persons[0]}
# empty result
echo ${persons[0][name]}
# empty result
echo persons[0]["name"]
# persons[0][name]
echo ${${persons[0]}[name]} #It could have worked if this work as a return
# Error

And I dont know what more try. Any help would be appreciated!

Thank you for reading!

Bash version: 4.3.48

3
  • 1
    bash doesn't support 2 dimensional arrays. Use perl, php, python etc. Commented Jun 22, 2017 at 10:28
  • @anubhava Then if i want for example do a curl and save the output to a variable could I access to some value of the variable? Commented Jun 22, 2017 at 10:55
  • Other languages will have their own libraries for fetching URLs internally; you won't need to execute an external program like curl. Commented Jun 22, 2017 at 11:24

1 Answer 1

1

The notion of a multi-dimensional array is not supported in bash, So

${persons[0][name]}

will not work. However, from Bash 4.0, bash has associative arrays, which you seemed to have tried, which suits your test case. For example you may do it like below:

#!/bin/bash
declare -A persons
# now, populate the values in [id]=name format
persons=([1]="Bob Marley" [2]="Taylor Swift" [3]="Kimbra Gotye")
# To search for a particular name using an id pass thru the keys(here ids) of the array using the for-loop below

# To search for name using IDS

read -p "Enter ID to search for : " id
re='^[0-9]+$'
if ! [[ $id =~ $re ]] 
then
 echo "ID should be a number"
 exit 1
fi
for i in ${!persons[@]} # Note the ! in the beginning gives you the keys
do
if [ "$i" -eq "$id" ]
then
  echo "Name : ${persons[$i]}"
fi
done
# To search for IDS using names
read -p "Enter  name to search for : " name
for i in "${persons[@]}" # No ! here so we are iterating thru values
do
if [[ $i =~ $name ]] # Doing a regex match
then
  echo "Key : ${!persons[$i]}" # Here use the ! again to get the key corresponding to $i
fi
done
Sign up to request clarification or add additional context in comments.

6 Comments

So in this case, I could save the id as Index? but what if I want to search the id if I only have the name?
@AlbertoLópezPérez You can just reverse the for loop, wait for my edit.
Okay, this works in my case but if instead of ids were for example... country this would be different I think. If I'm wrong correct me please.
Well, if you have to have country->name combination, you can't go with this method.. Because country is not unique for an individual.
Yes but what i really wanted was something like: person[name]=Bob person[country]=Spain persons[0]=person To have a structured variable for later do a foreach or something and do something with the data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.