2

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
4
  • 2
    This doesn't address your question, but if you want the output to go to a file, why bother with the echo part? Just remove the backticks and echo. Commented Feb 25, 2017 at 3:00
  • Why not just do who | cut -d' ' -f1 | sort | uniq >> users.txt?
    – I0_ol
    Commented Feb 25, 2017 at 3:01
  • @EricRenouf Gee, I'm dumb! I thought it only works with echo. Thanks so much!
    – Erik
    Commented Feb 25, 2017 at 3:02
  • 1
    Please note, however, that you could get your original version to work by quoting your command substitution, 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.
    – Fred
    Commented Feb 25, 2017 at 3:32

3 Answers 3

5

Actually, echo will work just fine on multi-line strings, if you actually provide a multi-line string by preventing word splitting from occurring. You do that by enclosing the command substitution inside double quotes, which block word splitting, but not expansions.

This will work (though it is uselessly verbose):

echo "`who | cut -d' ' -f1 | sort | uniq`" > users.txt

Of course, here the right solution is to avoid the command substitution (the backticks, equivalent to enclosing inside $()) as suggested in the other answer, but there are many cases where using a quoted command substitution (i.e. to pass arguments to a command other than echo) is useful.

1
  • 1
    This should be the accepted answer. Commented Feb 25, 2017 at 3:59
3

The reason that happens is that the results of the command are being passed through the word splitting stage of bash processing. All the pieces that are separated by any character in IFS will be separated into different words, so you're ending up with each name as a single argument to echo.

As noted though, if you just want the output of commands to go into a file, echo will often get in the way, and it isn't necessary. You can redirect the output of any command to a file, not just echo, so you could just redirect the output of the last command in your pipe like

who | cut -d' ' -f1 | sort | uniq > users.txt
0
1

Assuming you are only sorting to help with the uniq option, awk would be a better option:

who | awk '!_[$1]++{print $1}' > users.txt

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