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
  • 67,283
  • 35
  • 116
  • 255

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
  • 333,661
  • 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