I've been trying to output a command to a file like this:
echo `who | cut -d' ' -f1 | sort | uniq` > users.txt
But the contents of users.txt
is on one line.
However, if I just execute the command like this:
who | cut -d' ' -f1 | sort | uniq
The output is on multiple lines.
And if I use echo to output the command like this:
echo `who | cut -d' ' -f1 | sort | uniq`
I get the output on 1 line.
What is going on here? How do I write the output of this command onto a file with multiple lines?
Any help is highly appreciated.
Edit:
Thanks to a few comments, I realized I don't need the echo, and I can output it like this:
who | cut -d' ' -f1 | sort | uniq > users.txt
echo
part? Just remove the backticks and echo.who | cut -d' ' -f1 | sort | uniq >> users.txt
?echo "$(who | cut -d' ' -f1 | sort | uniq)" > users.txt
(replaced backticks with$()
to allow code formatting). While not the best way to achieve your stated purpose, it is useful to understand why it works, the reason being that quotes prevent word splitting from occurring.