-1

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?

Kusalananda
  • 333,661

1 Answers1

3

Redirections are processed first and separately from the command and its arguments. The placement of the redirection in a single command is arbitrary.

The command that you show,

cat demo.txt >> cat sample.txt

is equivalent to

cat demo.txt sample.txt >> cat

or, if you will, to

>> cat cat demo.txt sample.txt

The command, as a whole, appends the concatenation of the two files demo.txt and sample.txt to a file called cat.

The shell will first open cat (the file you redirect into) for appending to, creating it if it does not already exist, and will then use that as the standard output stream for the command cat demo.txt sample.txt.

Related:


I tend to write redirections tightly, with no whitespace between the redirection operator and (in this case) the file. I also tend to write redirections at the end.

cat demo.txt sample.txt >>cat

Some people like writing input redirections at the start of commands, as in

<file.in cat >file.out

But I don't personally use that as it looks weird to me.

Kusalananda
  • 333,661
  • Thank you for such a well-articulated response! So am I correct to infer that only one 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:41
  • @noriloxic You have given the command cat two files (not three). The shell has used >>cat as an instruction to redirect the output of the cat command into a file called cat (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 the cat command. – Kusalananda Apr 24 '22 at 21:47
  • I found a file named cat in my directory, which I did not think to look for until you told me about a file called cat ! 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
  • @noriloxic You created the file when you ran your command, asking the shell to redirect the output to that file. When you use > or >> with a string like cat 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 called cats. There is no issue with having files called the same thing as commands. – Kusalananda Apr 24 '22 at 21:57
  • @noriloxic: 'commands' aren't magic. Most commands in Unix actually are programs which are files, though system-supplied programs are in system directories you don't normally look at. You can look at them, and can even modify them although that is likely to break your system. Try type cat to see if cat 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
  • @dave_thompson_085 Thank you for the insight. the only mental model I have of commands are that they're like functions or methods where you pass arguments in via the parameter feed. So when I play around with calling 'cat' on the CLI, I want to figure out how many parameters I can pass through it, and how redirection symbols work with it. The fact that Kusalananda says the redirection symbol can be placed in quite a few locations and the fact that only the first 'cat' is recognized as a command means there's a lot of work I have to do to understand what it is I'm working with here. – noriloxic Apr 27 '22 at 19:16