Let's say I have a MySQL table animals
with columns id, name
and 3 rows:
1, Mountain Goat
2, Angry Chicken
3, Weird Llama
If I run the command animals=$(mysql -u root -e 'SELECT name FROM animals')
, I get the result Mountain Goat Angry Chicken Weird Llama
.
If I hard code the animals into the array animals=("Mountain Goat" "Angry Chicken" "Weird Llama")
, and then try to access the second entry of the array with the command echo ${animals[1]}
, I get the output Angry
, not "Angry Chicken".
Ultimately what I want is the pass each value animals.name
into a function in BASH. See the sample script below.
#!/bin/bash
animals=$(mysql -u root -e 'SELECT name FROM animals')
function showAnimal {
echo "Wow, look at the "$1"!";
}
for i in $animals; do
showAnimal ${animals[$i]}
done
showAnimal
And get the following results:
Wow, look at the Mountain Goat!
Wow, look at the Angry Chicken!
Wow, look at the Weird Llama!