Let say we have a text file demo.txt that only contains the word 'demo' and a text file sample.txt that only contains the word 'sample'.
I understand that cat demo.txt >> sample.txt
would append the contents of demo.txt into the contents of sample.txt.
But what happens when I have the cat command on both sides of the redirection symbol? I've tried the same thing with single and double redirection. I've studied enough to know that you really want to use the double redirection over the single if you don't want something overridden. I also know that the cat command is short for concatenation (it was a surprise to me that the 'cat' command can double as a txt print "function").
What is it that I'm not understanding about how this works that I feel cat demo.txt >> cat sample.txt
should produce results?
cat
command is recognized? The second one is treated as a file. The shell thinks I've given it three files when I've really [tried] to give it two!Also, the fact that the order of the items doesn't matter is going to take some getting used to! I'm certainly not accustomed to such flexibility!
– noriloxic Apr 24 '22 at 21:41cat
two files (not three). The shell has used>>cat
as an instruction to redirect the output of thecat
command into a file calledcat
(appending the data). Note that in the very last command in my answer (<file.in cat >file.out
),cat
is executed with no files as arguments. The input and output is handled by the shell, outside of the control of thecat
command. – Kusalananda Apr 24 '22 at 21:47cat
in my directory, which I did not think to look for until you told me about a file calledcat
! How does it know which file is the destination file for the append?cat
was never meant to be a file.... I thought I was using a command/function. – noriloxic Apr 24 '22 at 21:53>
or>>
with a string likecat
or whatever other string, that string will be used as a filename (this is broadly true, without going into too complicated topics). Using>> cats
would have created (or appended to) the file calledcats
. There is no issue with having files called the same thing as commands. – Kusalananda Apr 24 '22 at 21:57type cat
to see ifcat
is a program on your system and where -- on most it is/bin/cat
or/usr/bin/cat
. Shell can run programs you create yourself, in files, in the same way(s) as system programs. – dave_thompson_085 Apr 25 '22 at 03:31