1

I have a text file with some lines I'd like to sort. This file is named somefile.

cat somefile | sort

results in some sorted output being sent to stdout.

cat somefile > anotherfile

results in the output of cat somefile being written to anotherfile

However

cat somefile | sort > somefile

results in somefile being empty.

Why is this? I expect somefile to be sent to stdout, redirected into the sort program which sends sorted output to stdout which is then written to somefile.

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
ckea
  • 11
  • 1

3 Answers3

2

Processes in a pipeline are executed in parallel, not sequentially.

So what cat somefile | sort > somefile does is:

Concurrently run cat and sort with cat's stdout connected to sort's stdin and both cat's stdin and sort's stdout connected to file descriptors opened for somefile.

The shell needs to setup the redirections for < and > before cat and sort can run. In this process, it uses open() and dup2() system calls for each redirection. So cat does not get a chance to read from the file before the open() system call for the > truncates it.

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
0

The > redirection first empties the target file, so there's nothing to cat or sort.

choroba
  • 231,213
  • 25
  • 204
  • 289
0

This can be done using only the sort command.

sort -o somefile somefile

Otherwise you will have to run your sort to a tmp file

sort somefile > tmpfile && mv tmpfile somefile
goose goose
  • 86
  • 3
  • 15