144

Examples:

  • Create an ISO image and burn it directly to a CD.

    mkisofs -V Photos -r /home/vivek/photos | cdrecord -v dev=/dev/dvdrw -

  • Change to the previous directory.

    cd -

  • Listen on port 12345 and untar data sent to it.

    nc -l -p 12345 | tar xvzf -

What is the purpose of the dash and how do I use it?

codeforester
  • 39,467
  • 16
  • 112
  • 140
chemila
  • 4,231
  • 4
  • 22
  • 18

5 Answers5

151

If you mean the naked - at the end of the tar command, that's common on many commands that want to use a file.

It allows you to specify standard input or output rather than an actual file name.

That's the case for your first and third example. For example, the cdrecord command is taking standard input (the ISO image stream produced by mkisofs) and writing it directly to /dev/dvdrw.

With the cd command, every time you change directory, it stores the directory you came from. If you do cd with the special - "directory name", it uses that remembered directory instead of a real one. You can easily switch between two directories quite quickly by using that.

Other commands may treat - as a different special value.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 22
    note you can also use `-` for git branches, i.e. you can switch back to your previous branch with `git checkout -` – snappieT Jul 03 '14 at 13:08
  • 4
    Where in the tar docs does it explain that it is supported? How do I know which commands support it? –  Oct 27 '16 at 10:37
  • 2
    If a command has a special meaning for `-`, you can find its explanation in the man page. – codeforester Oct 31 '17 at 15:50
  • su - #make the shell a login shell – atongsa Sep 27 '18 at 08:17
  • I find a command like this very curious:tar -cvf - /home | aescrypt -e -p apples - >backup_files.tar.aes which is in fact tar -cvf - /home | aescrypt -e -p apples -o backup_files.tar.aes - because the piping char | should make the presence of the last dash unnecessary. But without the last dash the command fails. Sounds illogical. Can someone please give me a reference where the naked dash meaning in command lines is explained. I cannot find any. –  Jun 26 '20 at 11:02
  • @elmclose I'm a novice myself but the way I understand it: A pipe is a built-in construct. It connects stdout from the 1st command to the stdin of the 2nd command. However, it's up to the author of the command to decide what to do with stdin. There's no guarantee that every command actually reads stdin but `-` has become a convention to say "you can put a file path for this argument, but if you put `-` we'll read stdin instead". – Kenmore Feb 01 '21 at 05:12
  • @elmclose: some programs don't assume stdout as output. For example, `tar` will use the `$TAPE` environment variable or, if that doesn't exist, a baked-in default (see output of `tar --show-defaults`). GNU tar has `-f-` in there so will assume stdout, but that's not necessarily always the case. In any case, it's overridden by `$TAPE`. Explicitly specifying `-f -` on the command line will override everything. – paxdiablo Feb 01 '21 at 05:42
31

It's not magic. Some commands interpret - as the user wanting to read from stdin or write to stdout; there is nothing special about it to the shell.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 10
    Except in the case of `cd -`; `bash(1)` handles `cd -` as if you had written `cd $OLDPWD`. – sarnold Nov 08 '11 at 03:11
  • 6
    @sarnold But the point is the same, `cd` -- a shell built-in command -- interprets a dash itself. No "magic" :-) –  Nov 08 '11 at 03:12
  • 3
    @pst, sure, no magic (it's all just code :), but there _is_ something special about it to the shell. – sarnold Nov 08 '11 at 03:13
20

- means exactly what each command wants it to mean. There are several common conventions, and you've seen examples of most of them in other answers, but none of them are 100% universal.

There is nothing magic about the - character as far as the shell is concerned (except that the shell itself, and some of its built-in commands like cd and echo, use it in conventional ways). Some characters, like \, ', and ", are "magical", having special meanings wherever they appear. These are "shell metacharacters". - is not like that.

To see how a given command uses -, read the documentation for that command.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1
    daaamn! I got lost in `file` documentation and found this: `to test the standard input, use ‘-’ as a filename argument`. You're 100% correct. thank you – Eugene Aug 16 '22 at 13:26
8

It means to use the program's standard input stream.

In the case of cd, it means something different: change to the prior working directory.

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
6

The magic is in the convention. For millennia, people have used - as a prefix to distinguish options from arguments, and have used - as a filename to mean either stdin or stdout, as appropriate. Do not underestimate the power of convention!

William Pursell
  • 204,365
  • 48
  • 270
  • 300