When I execute a subshell to obtain output of a command, the line breaks are lost.
For example:
filenames=$(grep 'foobar' /some/dir)
echo $filenames
Assuming there is more than 1 file in /some/dir
that contains the string "foobar", those filenames will be printed in one long space-separated line instead of 1 filename per line.
I can't just use tr
to convert the spaces back to line breaks since the filenames could have spaces in them anyway.
Is there a way to maintain the line breaks?
Quote the variable to print the newlines:
filenames=$(grep 'foobar' /some/dir)
echo "$filenames"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With