cat redirection
Because of the order how things are done.
When you do:
cat abc > abc
>
is the output redirection operator, when the shell sees this it opens the file in truncation mode using O_TRUNC
flag with open(2)
i.e. open("abc", O_TRUNC)
, so whatever was there in the file will be gone. Note that, this redirection is done first by the shell before the cat
command runs.
So when the command cat abc
executes, the file abc
is already truncated hence cat
will find the file empty.
Adding to @heemayl's answer, if you want the code to be more clear about the sequence in which things are happening you can simply put the any redirections at the start of the command:
> abc cat abc