3 ms·
It’s not really subtle I think. When you redirect to a file, the file is opened for writing by the shell before it runs the program that you are invoking. This
by codetrotter 6y ago
It’s not really subtle I think. When you redirect to a file, the file is opened for writing by the shell before it runs the program that you are invoking.
This is also the same reason that if you try something like the following:
grep whatever somefile >somefile
You always end up with somefile being empty, whereas what you thought you were doing was replacing the contents of the file with only the lines in the file that contain the word “whatever”. But instead the shell will truncate the file first and then grep is searching through an already empty file and therefore there is nothing to find and the file ends up empty.