ls > ls.out
this will include ls.out in the list too.
My understanding is: > (shell output redirection operator is creating a file first (to take the STDOUT) if it is not already existing and then ls command is coming to play and it is including the just created ls.out file in the output.
Is this correct? If not, can you please elaborate the workings of that command.
Similarly
wc temp > temp
Will print 0 0 0 temp inside the just created temp file.
This behavior of shell is interesting. I wonder, how it is actually working. BTW, both are exercises in The Unix Programming Environment Book. As I mentioned the answer above, I want some expert to correct my understanding.
When redirecting standard output with
>, the shell will create the file (as an empty file) if it doesn’t already exist. Further, that file is opened before the shell forks and execs the command being executed. When you dowc temp > temp, the shell opens the file for writing and then forks and execswc, passing on its open file handles in the process.