1

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

1 Answer 1

2

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"))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! would you know how to append data with that by any chance? I tried += after the variable but it resulted in all data being placed in the first row of the array [0]
@lacrosse1991 No, saying IFS=$'\n' something_array+=($(echo -e "$something")) should append.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.