Capture Multiple Line Output Into a Bash Variable

Posted on August 11, 2019 at 11:50 am

Lets say you have a bash script “run.sh” that outputs multiple lines, i.e:

line1
line2
line3

You capture the script output in a bash variable like this:

OUTPUT=$(/root/run.sh)

Now pay attention on how you echo the output:

Echo Variable Without Double-Quotes

If you echo the variable without the double-quotes:

echo $OUTPUT

It will output the multiple lines in a single line, i.e:

line1 line2 line3

Echo Variable With Double-Quotes

If you echo the variable with the double-quotes:

echo "$OUTPUT"

It will preserve internal spacings and new-lines, i.e:

line1 
line2 
line3

Receive updates via email

Other Posts

Updated Posts