I have a variable:
var='/path/to/filename.ext
/path/to/filename2.ext
/path/to/filename3.ext'
I want to put all strings separated by a newline in an array:
declare -a arr
Based on numerous posts here on StackOverflow, I found a couple of ways:
# method 1: while loop
while read line; do
arr+=($line)
done <<< "$var"
# method 2: readarray
readarray -t arr <<< "$var"
# method 3:
IFS=$'\n'
arr=("$var")
However, before I learned all these methods, I was using another one, namely:
# method 4 (not working in the current situation)
IFS=$'\n'
read -a arr <<< "$var"
This is not working, because it will only store the first string of var in arr[0]. I don't understand why it doesn't work in situations where the delimiter is a newline, while it does work with other delimiters, e.g.:
IFS='|'
strings='/path/to/filename.ext|/path/to/filename2.ext|'
read -a arr <<< "$strings"
Is there something that I'm missing?
Edit
Removed my own answer that argued you cannot use read for this purpose. Turns out you can.
$in front of the'\n'?