I can't differentiate between these two lines of code as the output for each command is same
cat volcanoes.txt
cat < volcanoes.txt
I can't differentiate between these two lines of code as the output for each command is same
cat volcanoes.txt
cat < volcanoes.txt
<
reads a file (specified on the Right-Hand Side) and pipes it into the STDIN of the command on the Left-Hand Side.
cat
takes input and outputs it to STDOUT.
If you provide an argument to cat
, it takes input from there. Otherwise, it takes it from STDIN.
It isn't usually useful to use <
in conjunction with cat
.
cat volcanoes.txt
passes volcanoes.txt as an argument, which cat
will attempt to locate on disk and open.
cat < volcanoes.txt
runs cat
with no arguments, and the interpreter opens volcanoes.txt as cat
's stdin.
For a clearer example, try testing with multiple files:
echo 1 > a
echo 2 > b
now you can see the difference by comparing
grep . a b
vs
cat a b | grep .
In the first one, a
& b
are passed as arguments, so grep
opens them itself and knows the source of each line of data, so it tells you which file each line came from.
$: grep . a b
a:1
b:2
Done the second way, cat
reads both files and puts the content on grep
's stdin as a single anonymized stream, much the same as you did with a single file when you said cat < volcanoes.txt
. This way, grep
only knows data is coming on stdin, and it can't give you the additional info.
$: cat a b | grep .
1
2
For cat
, it's functionally the same because of what cat
is and does, but it's still mechanically different, and for some programs the difference could be crippling, or at least relevant.