This BASH script works as expected. I have a variable ($foo) that contains lines of data, I tell bash to separate at new lines, and I loop through the variable.
#!/bin/bash
foo="
Original Line1
Original Line2
Original Line3
"
IFS=$'\n'
for line in $foo
do
echo $line
done
As expected, the data is on it's own line.
Original Line1
Original Line2
Original Line3
I add this line to the script, to replace the text "Line2" with "EXAMPLE" in the $foo variable.
foo=$( echo $foo | sed "s|Line2|EXAMPLE|" )
When I run the script, the data is no longer separated on it's own line, and instead prints on a single line. I am not sure how I can replace values in a variable and retain new lines.
Original Line1 Original EXAMPLE Original Line3
echo "$foo" | ...
. See Why does my shell script choke on whitespace or other special characters? – steeldriver Oct 11 '18 at 12:24foo=("Original Line1" "Original Line 2" ...)
– Jeff Schaller Oct 11 '18 at 12:48