140

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?

5 Answers 5

148

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.

7
  • 20
    note you can also use - for git branches, i.e. you can switch back to your previous branch with git checkout -
    – snappieT
    Jul 3, 2014 at 13:08
  • 4
    Where in the tar docs does it explain that it is supported? How do I know which commands support it?
    – user146043
    Oct 27, 2016 at 10:37
  • 2
    If a command has a special meaning for -, you can find its explanation in the man page. Oct 31, 2017 at 15:50
  • su - #make the shell a login shell
    – atongsa
    Sep 27, 2018 at 8: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.
    – user9224371
    Jun 26, 2020 at 11:02
30

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.

3
  • 10
    Except in the case of cd -; bash(1) handles cd - as if you had written cd $OLDPWD.
    – sarnold
    Nov 8, 2011 at 3:11
  • 6
    @sarnold But the point is the same, cd -- a shell built-in command -- interprets a dash itself. No "magic" :-)
    – user166390
    Nov 8, 2011 at 3:12
  • 3
    @pst, sure, no magic (it's all just code :), but there is something special about it to the shell.
    – sarnold
    Nov 8, 2011 at 3:13
19

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

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

6

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

2
  • Do not overestimate people's ability to discern manners of speech from literal expressions. This has not been going on for millennia.
    – tripleee
    Jan 14, 2020 at 19:07
  • 2
    @tripleee - True, but it has been going on for billions of seconds. ;-) Jan 15, 2020 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.