2

I can't differentiate between these two lines of code as the output for each command is same

cat volcanoes.txt 
cat < volcanoes.txt
oguz ismail
  • 1
  • 16
  • 47
  • 69
B Luthra
  • 153
  • 9
  • I wonder if this could be read as a subset of [pipe, standard input and command-line arguments in bash](https://stackoverflow.com/questions/1504867/pipe-standard-input-and-command-line-arguments-in-bash). – Charles Duffy Apr 14 '19 at 14:17

2 Answers2

4

< 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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It can be useful to control cat's stdin. `foo | cat prefix.txt -` emits `prefix.txt` followed by the output of `foo` -- a use case that's actually in line with `cat`s description and mission in life of *concatenating* multiple inputs. If you only have *one* argument to cat (which the "an argument" wording implies to be typical/usual), you typically don't have a good reason to use cat at all. – Charles Duffy Apr 14 '19 at 14:13
1

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.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36