Both of the following commands try to open a non-existing file foo, but the error messages are a little different. What could be the reason?

$ cat foo
cat: cannot open foo
$ cat < foo
-bash: foo: No such file or directory
  • 12
    Guys, please do not downvote posts only based on formatting, especially for posts coming from newcomers. Here, it's a very valid question. – Stéphane Chazelas Nov 24 '16 at 11:22
  • 21
    It should be cat < food instead of cat < foo – Tulains Córdova Nov 24 '16 at 13:52
  • 6
    @TulainsCórdova Or, in some cultures, food < cat ;) – DepressedDaniel Nov 25 '16 at 0:15
  • 1
    Loosely related: What are the shell's control and redirection operators? – G-Man Nov 25 '16 at 3:28
  • 1
    Only the first error comes from cat. Now, I wonder where the second message comes from, considering that it starts with bash:... – Dmitry Grigoryev Nov 25 '16 at 14:12
cat foo

This runs the cat command with argument foo. The error printed onscreen depends entirely on what was decided by the programmer of the command.

cat < foo 

This feeds the contents of the file foo to the cat command by using the Bash stdin redirection. If the file doesn't exist, it is Bash that complains about it.


In $ cat foo the shell (here bash) executes the cat command and passes the parameter foo. The cat program chooses to interpret that parameter as a file name - and tries to open the file. The error you see is from the cat program which (naturally) cannot open the file.

The version $ cat < foo is a redirection which is handled by the shell. < is a shell operator which instructs the shell to open a file and redirect it to stdin. The file does not exist so you get a "No such file". This time the error comes from the shell (bash) and looks a little different.

This is why you see 2 different errors. The cause is the same - but it is from 2 different programs (cat and bash).

  • 18
    No. in cat < foo, cat is not invoked if the redirection fails. That (and having consistent error message) is one reason why it's often better to use redirection where possible. Also consider the cat < in > out vs cat in > out where the former prevents out to be overridden if in doesn't exist (the shell cancels the command right after the failing < in redirection and doesn't do the next > out redirection, let alone invoke cat). – Stéphane Chazelas Nov 24 '16 at 11:19
  • Cool! Pure logic. I'll edit my nonsense. – Claus Andersen Nov 24 '16 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.