4

If I get the result of a command on a variable, how can I print this output with new lines. Silly example:

XX=$(ls -l); echo $XX

When I execute the above sentence, I have one line ilegible result instead of a formatted return as I saw when ls -l is executed on a terminal. Is there any way to get the result of a command formatted or to display it's result with newlines?

jherran
  • 3,939

1 Answers1

5

You need double quotes to make the shells don't perform field splitting:

XX="$(ls -l)"; echo "$XX"

But it's not good to use echo with variable that you don't know its content, you should use printf (read this answer) instead:

XX="$(ls -l)"; printf '%s\n' "$XX"
cuonglm
  • 153,898