Hello i want to ask why when i write
there is new line after every row but when i assign it to a variable
and than echo that variable
i get everything in concatanated in one row...Thanks
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
There are two independent issues arising from this question.
ls command is built to act differently when it writes to the terminal and when it writes to a file or pipe. You can see this by comparing these two commands
ls ls | cat
In the first example,
ls can see it's writing to the terminal and so it formats its output for a human reader. In the second example,
ls can see it's writing to a file or pipe, so it produces one entry per line. You can override the two settings with the
When you assign
ls -l to a variable the newlines are kept. However, in your case you're not quoting the variable when you use it so each run of "whitespace" (everything that doesn't print visible characters) is converted to single space. Compare these results
var=$(ls -l); echo $var var=$(ls -l); echo "$var"