I realized that cat sth
and cat < sth
both happen to give same output. Are they the same?

- 56,709
- 26
- 150
- 232

- 143
-
See also Input from file: “advanced” (using less-than sign) vs. “beginner” syntax – don_crissti Jul 08 '16 at 19:20
3 Answers
They're not completely identical. In cases where it matters, the redirection approach will generally give more annoying and obscure results (of course that might be what you want though).
$ cat < /proc/self/maps
$ cat /proc/self/maps
55c61257e000-55c61258a000 r-xp 00000000 fd:00 1180143 /usr/bin/cat
...
Or try grep, the search program
$ grep "grep" /proc/self/exe
Binary file /proc/self/exe matches
$ grep "grep" < /proc/self/exe
$
Bit of a cheat, but you're most likely to run into this when using sudo
, to run commands with root
access:
$ sudo cat < /etc/shadow
bash: /etc/shadow: Permission denied
$ sudo cat /etc/shadow
root:!::0:99999:7:::
....

- 50,249
-
4Your sudo example makes an excellent point; it may be worth mentioning that the second method works because it is the super-user, as
sudo cat
, opening the file, while it is the non-super-user's shell in the first case that attempts and fails to open the file. – dhag Jul 08 '16 at 18:38
cat
's job is it to concatenate everything it gets and then to print it out on stdout yet often is used to print out the contents of a single file (which of course is just concatenating the contents of all the one files given to it and then writing it to stdout). stdout (standard output) is just programs usually write output data. (There are programs (like dd
) which write their output data to stdout unless stated differently by parameters and there also is the standard error channel stderr which programs write to when an error occurs.)
cat
can take data given to it via stdin (standard input) or data from files passed to it as parameters.
What you're doing with cat sth
is to call cat
with the single parameter sth
telling cat
to write everything in the file sth
to stdout. You could add more file names and see that the content of the files is concatenated and then written to stdout.
cat < sth
changes the stdin of cat
to the contents of the file sth
before calling cat
. cat
then just reads everything from its stdin and writes it to stdout.
In both cases, cat
writes the contents of the file sth
to stdout. You can read more about this here and also type in man cat
into a terminal to read the man page of cat
describing what it does.
Note that the contents of sth
are read by the user you are logged in as (who you are logged in as in the terminal is not necessarily who you are logged in as in the graphical session (if any)), whereas programs can be executed with different rights by writing sudo
before them and then read with the rights of the user who executed them. This can lead to different outcomes because the user you are logged in as and the user you switch to with sudo
(not necessarily root
, you can specify otherwise) might have different access rights on the file you're trying to read from.

- 67,283
- 35
- 116
- 255

- 3,237
In the specific case of cat
, printing the file sth
works just as
well using cat sth
or cat <sth
, because the cat
command was
designed to either print all files given as command line parameters
(the cat sth
case: cat
will receive the string sth
as a
parameter, and will open the file) or, if no files were given, print
standard input instead (the cat <sth
case: the shell will honor the
redirect by opening sth
and connecting it to cat
's standard
input).
So the behaviour being the same in both cases is specific to cat
; it
may not work at all for commands designed to accept only one of
standard input or command line parameters. In other cases, the
behavior may be slightly different, since the command may not have
access to the file name when the data is obtained through standard
input; compare:
$ grep -H foo sth
sth:foo
$ grep -H foo <sth
(standard input):foo

- 15,736
- 4
- 55
- 65