There are multiple global variables with same pattern in my shell script like:
VALID_DOMESTIC_ANIMAL=(cat dog)
VALID_ACQUATIC_ANIMAL=(shark dolphin)
Now, There is another argument CATAGORY , which could be ACQUATIC/DOMESTIC. How can I select the expected global variable to iterate ? I tried following code:
#!/bin/bash
VALID_DOMESTIC_ANIMAL=(cat dog)
VALID_ACQUATIC_ANIMAL=(shark dolphin)
CATAGORY=ACQUATIC
VALID_CATAGORY_ANIMAL=VALID_${CATAGORY}_ANIMAL
#echo "VALID_CATAGORY_ANIMAL ${VALID_CATAGORY_ANIMAL}"
for anime in "${VALID_CATAGORY_ANIMAL[@]}"; do
echo "${anime}"
done
I am expecting output shark dolphin but I am getting VALID_ACQUATIC_ANIMAL
How can I tell bash script that if there exists a variable with same name, use it ?
VALID_CATAGORY_ANIMALas a nameref - see this related Q&A from yesterday Varibles - Unionfoo=(cat, dog)assigns the valuescat,anddog. The comma isn't needed here to separate the two words, but ends up as part of the first word. (Also, "category" and "aquatic".)