Bash Scripts – How to Separate Command Output to Individual Lines

bashscripts

list=`ls -a R*`
echo $list

Inside a shell script, this echo command will list all the files from current directory starting with R, but in one line. How can I print each item on one line?

I need a generic command for all the scenarios happening with ls, du, find -type -d, etc.

Best Answer

If the output of the command contains multiple lines, then quote your variables to preserve those newlines when echoing:

echo "$list"

Otherwise, the shell will expand and split the variable contents on whitespace (spaces, newlines, tabs) and those will be lost.