It is well known that you cannot use the same file for input and ouput in the same command. For instance,
$ cat header.txt body.txt >body.txt
doesn’t do what you want. By the time cat
reads body.txt
, it has already been truncated by the redirection and it is empty. The final result will be that body.txt
will hold the contents of header.txt
only.
One might think to avoid this with process substitution, that is, that the command
$ cat header.txt <(cat body.txt) > body.txt
will force the original contents of body.txt
to be somehow saved in some buffer somewhere before the file is truncated by the redirection. It doesn’t work. The cat
in parentheses begins reading the file only after all file descriptors have been set up, just like the outer one. There is no point in trying to use process substitution in this case.
The only way to prepend a file to another file is to create an intermediate one:
$ cat header.txt body.txt >body.txt.new
$ mv body.txt.new body.txt
which is what sed
or perl
or similar programs do under the carpet when called with an edit-in-place option (usually -i
).