Intro to Linux cli; output redirection

59 Views Asked by At

Why does cp a b result in error when cp a b > a results in no error when neither a nor b file exists.

From my understanding cp a b gives error because neither file exists, makes sense and cp a b >a seems to create both files and copy blank from b to a but the reason a is empty is because output of the operation does not actually produce any messages.

Please let me know what the correct explanation is. Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

When you invoke cp a b > a, the first thing the shell does is create the file a. It then creates a child and redirects its output to that file before the child execs cp. So a exists before cp starts, and it happily copies the file.