1

Here is something I don't understand.

Why this works : echo "akka" | cat

But this does not produce "akka" on the console : echo "akka" > cat

And this does not even work : cat < echo "akka"

For me they should be the same. But these 3 commands seem different

AntonBoarf
  • 1,239
  • 15
  • 31

1 Answers1

3

You are confusing the differences between file redirection and piping.

The pipe symbol | is used to pass the output of one command into another command.

Meanwhile, < and > are used for file redirection.

These are very different operations.

Example 1:

echo "akka" | cat

The echo command has the output akka, and this is piped into the standard input of the cat command. The cat command writes to standard output, so in this case it prints akka. Of course, this is no different from doing simply:

echo "akka"

Example 2:

echo "akka" > cat

The echo command has the output akka. Using >, this output is then redirected into a file called cat. There is no output shown in the terminal in this case, since the output is placed into a file instead.

Example 3:

cat < echo "akka"

This is quite different from the first two. This runs the cat command, which reads from standard input. Using <, input is passed to the cat command from a file called echo. If no such files exists, then it will produce an error.

costaparas
  • 5,047
  • 11
  • 16
  • 26
  • @charles I actually couldn't find a suitable duplicate in this case. If you can find one, please suggest it. Are you saying the question is not well-asked? – costaparas Jan 25 '21 at 23:55
  • 1
    Working on the duplicate list now. – Charles Duffy Jan 25 '21 at 23:56
  • @charles though the linked duplicate is related, I feel like my answer provides a more relevant discussion that covers the 3 cases in the question. I did search for duplicates and found that, but I didn't feel it addressed this question well enough, particularly the 3rd case mentioned by the OP. Hence, why I provided an answer in this case. – costaparas Jan 25 '21 at 23:59
  • 2
    Yup. After spending a bit more time looking at the difference between this question and its predecessors, I do think answering is defensible here; for that matter, I sometimes add a community-wiki answer myself when there's something that's duplicative, but where it's not immediately obvious how to apply the duplicates. – Charles Duffy Jan 26 '21 at 00:01
  • ok thanks. So basically 'cat < myFile' is the same as 'cat myFile', correct ? – AntonBoarf Jan 26 '21 at 00:08
  • Not exactly. `cat < myFile` will use file redirection to pass the contents of the file `myFile` to the standard input of `cat`, while `cat myFile` will pass the filename to `cat` (as a command-line argument), and cat will open the file and read it. In the second case, the `cat` command has the filename, while in the first case, the `cat` command doesn't deal directly with the file and only reads from standard input. – costaparas Jan 26 '21 at 00:19
  • Though they do achieve the same thing in this case, the difference is with the latter, you can supply multiple filenames as arguments and `cat` will concatenate the contents of those files (e.g. `cat myFile1 myFile2`), while using the former, you pass the contents of just one file. – costaparas Jan 26 '21 at 00:19