Issue
When I execute echo 123 > a.txt b.txt
I expect to see 123
written to two files, a.txt
and b.txt
. But the actual result is that only one file is created. The content 123 b.txt
is written in the file called a.txt
. Why does this happen?
Solution
Two things are needed to make sense of this:
- The redirection operator
>
only applies to a single file name. Anything after that file name is not part of the redirection; it's an additional argument to theecho
command. - A lesser known fact is that redirections can be written anywhere in a command and have the same effect.
Putting these facts together, it means these commands are all equivalent:
echo 123 b.txt >a.txt # traditional order
echo 123 >a.txt b.txt # your command
echo >a.txt 123 b.txt
>a.txt echo 123 b.txt
All of them run the command echo 123 b.txt
with output redirected to a.txt
.
(I've condensed > a.txt
to >a.txt
to make it easier to see what >
is binding to.)
Answered By - John Kugelman Answer Checked By - Timothy Miller (WPSolving Admin)