lacrosse1991
lacrosse1991

Reputation: 3162

how can I append a multi-line variable to an array

I have a multi-line variable in a bash script (contains output from a command that is executed automatically via SSH). How can I append this variable to an array so that each line in the variable is put into a new row/member of the array?

something="first row
           second row
           third row"

echo "${something_array[0]}" - first row
echo "${something_array[1]}" - second row
echo "${something_array[2]}" - third row

There "could" already be data inside the array, therefore I am try to append the lines to the array. I have already tried

IFS='\n' something_array=($(echo -e "$something"))

although I ran into some issues, plus it does not append the data either

Upvotes: 1

Views: 773

Answers (1)

devnull
devnull

Reputation: 123508

You wanted to use ANSI-C Quoting for defining IFS. Say:

IFS=$'\n' something_array=($(echo -e "$something"))

instead.

In order to append to the array, say:

IFS=$'\n' something_array+=($(echo -e "$something"))

Upvotes: 2

Related Questions