Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store multiple row output in a bash array?

I have a select statement

sqlplus [credentials] select variable from table;

It returns 6 rows and I need to store them as an array in bash array variable.

like image 449
batty Avatar asked Jul 27 '11 17:07

batty


2 Answers

array=(`sqlplus [credentials] select variable from table;`)
echo ${array[*]}
like image 137
jman Avatar answered Nov 15 '22 11:11

jman


If your variables contain spaces and you want the array to have an element for each line of output (as opposed to each word of output), you also need to set your IFS. And you may want to use quotes when using the array:

SaveIFS="$IFS"

IFS=$'\n'
array=( $(sqlplus [credentials] select variable from table;) )
echo "${array[*]}"

IFS="$SaveIFS"
like image 42
mivk Avatar answered Nov 15 '22 11:11

mivk