1

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?

2

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.