Do I need to generate a second file to sort a file? Do I need to generate a second file to sort a file? bash bash

Do I need to generate a second file to sort a file?


Try:

sort -o file.txt file.txt

See http://ss64.com/bash/sort.html

`-o OUTPUT-FILE'     Write output to OUTPUT-FILE instead of standard output.  If     OUTPUT-FILE is one of the input files, `sort' copies it to a     temporary file before sorting and writing the output to     OUTPUT-FILE.


The philosophy of classic Unix tools like sort includes that you can build a pipe with them. Every little tool reads from STDIN and writes to STDOUT. This way the next little tool down the pipe can read the output of the first as input and act on it.

So I'd say that this is a bug and not a feature.

Please also read about Pipes, Redirection, and Filters in the very nice book by ESR.


Because you're writing back to the same file you'll always end up with a problem of the redirect opening the output file before sort gets done loading the original. So yes, you need to use a separate file.

Now, having said that, there are ways to buffer the whole file into the pipe stream first but generally you wouldn't want to do that, although it is possible if you write something to do it. But you'd be inserting special tools at the beginning and the end to do the buffering. Bash, however, will open the output file too soon if you use it's > redirect.