I have a bash array as follows:
fruits=(
F001 "Sour fruit" 5
F002 "Sweet fruit" 15
F003 "Good fruit" 10
)
I want to access this array and print it as follows:
Fruit code: F001, Desc: Sour fruit, Price: 5
Fruit code: F002, Desc: Sweet fruit, Price: 15
Fruit code: F003, Desc: Good fruit, Price: 10
I tried the following code, But it's giving me a wrong output
for f in "${fruits[@]}"; do
IFS=" " read -r -a arr <<<"${f}"
code="${arr[0]}"
desc="${arr[1]}"
price="${arr[2]}"
echo "Fruit code: : ${code}, Desc: ${desc}, Price: ${price}"
echo
done
Anyine know how to do this :)
typeset -p fruits(ordeclare -p fruits) to see what's really in your array; as kvantour has noted ... you should see 9 items in the array, not the 3 you're expecting