1

I am just learning about shell commands; more specifically, I am learning about pipe.

Right now I cannot tell the difference between the following commands: ls | sort file.txt and sort file.txt. Apparently, there is supposed to be a difference.

Please can someone explain what is happening?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Jake Jackson
  • 113
  • 3
  • 1
    note that you shouldn't pipe the result of `ls`. See [Why *not* parse `ls` (and what to do instead)?](https://unix.stackexchange.com/q/128985/44425) – phuclv Aug 14 '20 at 14:18
  • That's a hefty read, but I'll be sure to give it a gander. My question comes from an example I was looking at, so it wasn't any child of my thoughts. Thank you for he reply though :) – Jake Jackson Aug 14 '20 at 14:27
  • @JakeJackson: Hopefully it was an example of what not to do because that command doesn't actually work. The output of `ls` is being discarded. – jesse_b Aug 14 '20 at 14:35
  • @jesse_b Yes, possibly. It asked what the difference was, but maybe they expected you to realise there was no difference. Either way, I understand now :) – Jake Jackson Aug 14 '20 at 14:57
  • Well there is a difference, `ls | sort file.txt` will spawn an extra process – jesse_b Aug 14 '20 at 14:58
  • Haha yeah, technically you're right – Jake Jackson Aug 14 '20 at 15:05

1 Answers1

4

The visible outcome of the two commands ls | sort file.txt and sort file.txt would be the same:

sort file.txt sorts the lines of file.txt lexicographically and outputs the result.

ls | sort file.txt calls ls to generate a list of the names in the current directory. That list is sent to sort file.txt. Since sort is reading from file.txt, it will ignore the list of names coming from ls and instead produce the sorted contents of file.txt as output. The output from ls is discarded since sort is not reading from its standard input in this instance.

You may have wanted to use ls | sort which would have sorted the lines of output from ls (which would have been sorted already). sort would have read the output of ls since it was not given any specific filename to read from and is therefore reading from its standard input stream (which is connected to the output stream of ls via the pipe). Note though that using a tool that expects lines of text may fail to handle Unix filenames as these may contain newlines.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • 1
    Thank you, my friend. I thought it was a load of rubbish, the example I was pulling this from. Pipe is not hard to understand whatsoever, but the example I was following made me question it. At least I know that the command does literally nothing - what I thought ti begin with. I just want to check. Stay safe :) – Jake Jackson Aug 14 '20 at 14:55
  • Note that in `ls | sort file.txt`, the output of `ls` is not read at all. If `sort` exits before `ls` has finished writing, `ls` will be killed by a SIGPIPE. One visual difference with `sort file.txt` could be the eventual error messages if any displayed on stderr by `ls`. – Stéphane Chazelas Aug 14 '20 at 15:21