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.
>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.