Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sort file.txt > file.txt is empty

I have a doubt about a simple command and it is because in bash if I do

sort file.txt > file.txt

file .txt remains empty (it shouldn’t keep file.txt with what it had before but sorted) but if I do

sort file.txt >> file.txt

then the ordered elements are added to the previous elements.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is the expected behavior. The phrase > file.txt opens file.txt for overwriting before the command sort is even run. So when sort runs it sees that file.txt is empty.

On the other hand, the phrase >> file.txt opens file.txt for appending before the command sort is even run. So when sort runs it reads the full file and then appends the results to the file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading