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?

1 Answer 1


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"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .