Output command

Hello

when i execute the following command

$sort list
i get the list sorted out onto my screen

if i use
$sort list >listsort
i get an file listsort with the list sorted out

If i use
$sort list >list
i get an empty file list

also when i use
$sort list | >list
i get an empty file

Can somebody tell me how to put the sorterd list immediatly in the same file 'list' ?

Thanks alot four your response !!

A ducatiloving scriptnewbie

If this is something that you will be doing a lot of in a script or something similiar, wrap the sort command in a shell script that sorts the list to a temp file and mv the temp file to the original filename.

This is a dangerous thing to do berty007. If things go wrong you will not have the source file anymore and thus repairing the damage would be a hassel (you would have to rebuild the source file!)

you could do a test to ensure the new file contains data (test -s <filename> ) before deleteing the original.

You absolutely need an input file and an output file. You can't get around that. You can remove the name from the input file and re-use it for the output file. Like this:
(exec < list ; rm list ; sort > list)

but like Ralf says, this is dangerous.

The reason that "$ sort list >list" leaves you with an empty file is that the shell sees that you want output to go to "list" (with >list). Before it even begins that command, it ither creates or truncates the output file. Now you just sorted the file you truncated.