Difference between “cat” and “cat <"

I was working through a tutorial and saw use of both cat myfile.txt and cat < myfile.txt. Is there a difference between these two sequences of commands? It seems both print the contents of a file to the shell.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In the first case, cat opens the file, and in the second case, the shell opens the file, passing it as cat‘s standard input.

Technically, they could have different effects. For instance, it would be possible to have a shell implementation that was more (or less) privileged than the cat program. For that scenario, one might fail to open the file, while the other could.

That is not the usual scenario, but mentioned to point out that the shell and cat are not the same program.

Method 2

There is no major visible difference in your test case. The most obvious one would be the error message you get if there is no file named myfile.txt in the current directory, or if you are not allowed to read it.

In the former case, cat will complain and in the latter case, your shell will, clearly showing which process is trying to open the file, cat in the former one and the shell in the latter one.

$ cat myfile.txt
cat: myfile.txt: No such file or directory
$ cat < myfile.txt
ksh93: myfile.txt: cannot open [No such file or directory]

In a more general case, a major difference is using redirections cannot be used to print the content of more than one file, which is after all the original purpose of the cat (i.e. catenate) command. Note that the shell will anyway try to open all files passed as redirected input, but only actually pass the last one to cat unless you use zsh and its multios “zshism”.

$ echo one > one
$ echo two > two
$ cat one two # cat opens one, shows one, opens two, shows two
one
two
$ cat < one < two # sh opens one then opens two, cat shows stdin (two)
two
$ rm one two
$ echo one > one
$ cat one two # cat opens and shows one, fails to open two
one
cat: two: No such file or directory
$ cat < one < two # the shell opens one then opens two, fails and 
                  # displays an error message, cat gets nothing on stdin
                  # so shows nothing
ksh93: two: cannot open [No such file or directory]

On a standard system, the shell and cat have no difference in file access rights so both will succeed of fail equally. Using sudo to raise cat‘s privileges will make a big difference in behavior, as Thomas Dickey reply and attached comments already suggested.

Method 3

cat myfile.txt reads the file myfile.txt then prints it to the standard output.

cat < myfile.txt here cat isn’t given any file(s) to open, so -like many Unix commands do- reads the data from the standard input, which is directed there from file.txt by the shell, and prints to standard output.

Method 4

@Thomas Dickey‘s answer is brilliant.

I just want to add some obvious facts about the case of reading several files (loosely related to your question, but still):

  • cat <file1 <file2 <file3 will read only file3, at least in bash. (Actually, it depends on shell, but most shells will dup every specified file to stdin, which causes the last one to effect.)
  • cat file1 file2 file3 will read all the specified files sequentially (actually cat is shortened form of the word concatenate).
  • cat file1 file2 file3 <file4 <file5 <file6 will read only file1, file2, file3 (as cat ignores stdin when filename arguments are passed).
    • cat file1 file2 - file3 <file4 <file5 <file6 will read file1, file2, file6, file3 (as hyphen forces cat not to ignore stdin).

And about errors. In case of inability to open some of files specified as arguments (without <), cat will skip failed files (with outputting relevant message to stderr), but still read other files. In case of inability to open at least one of files specified as redirections (with <), shell won’t even start cat (this occurs even for redirections actually not used by cat). In both cases erroneous exit code will be returned.

Method 5

We can use another command to notice the difference between:

wc –w food2.txt

Possible output:

6 food2.txt

The command tells the file name since it knows it (passed as an argument).

wc –w < food2.txt

Possible output:

6

The standard input is redirected to file food2.txt without the command knowing about the file’s name.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x