7

I am learning the cat command of linux, and I found this command :

$ echo 'Text through stdin' | cat - file.txt

What does "-" mean here? If I don't type it , then 'Text through stdin' will not be shown.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
huashui
  • 1,758
  • 2
  • 16
  • 24
  • possible duplicate of [Does " - " mean stdout in bash?](http://stackoverflow.com/questions/3797795/does-mean-stdout-in-bash) (admittedly for stdin/stdout, depending on the context...) – Bruno Aug 22 '13 at 13:23

3 Answers3

15

it is common to write stdin as dash (-).

even man cat mentions that:

With no FILE, or when FILE is -, read standard input.

and the manpage even has an example illustrating the use of dash and ordinary filenames (which is quite close to your original question, but includes the answer):

   cat f - g
          Output f's contents, then standard input, then g's contents.
umläute
  • 28,885
  • 9
  • 68
  • 122
  • 2
    It is important to realise that after `stdin` has been read the file `file.txt` will then be displayed. – PP. Aug 22 '13 at 13:23
  • @PP. right, i added two more lines of the manpage (incidentally they come just after my original quote), that should make this explicit. – umläute Aug 22 '13 at 13:38
5

- tells cat to read from stdin. This is quite common, a lot of apps read from stdin if you pass - to them.

Some apps use - as stdout.

Here is an example of downloading blender and instead of writing it to a file we write it directly to stdout and pipe it to tar, which expands it on the fly during download.

wget -c https://download.blender.org/source/blender-2.90.1.tar.xz -O - | tar -xzv

Here the -O - tells wget to write directly to stdout

hashier
  • 4,670
  • 1
  • 28
  • 41
3
$ echo 'Text through stdin' | cat - file.txt

- tells cat to read from standard input, in this case, from the pipe, i.e, what echo 'Text through stdin' outputs.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294