What's the magic of "-" (a dash) in command-line parameters?

64,245

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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!

Share:
64,245
chemila
Author by

chemila

nop

Updated on July 24, 2020

Comments

  • chemila
    chemila almost 4 years

    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?

  • sarnold
    sarnold over 12 years
    Except in the case of cd -; bash(1) handles cd - as if you had written cd $OLDPWD.
  • Admin
    Admin over 12 years
    @sarnold But the point is the same, cd -- a shell built-in command -- interprets a dash itself. No "magic" :-)
  • sarnold
    sarnold over 12 years
    @pst, sure, no magic (it's all just code :), but there is something special about it to the shell.
  • snappieT
    snappieT almost 10 years
    note you can also use - for git branches, i.e. you can switch back to your previous branch with git checkout -
  • Admin
    Admin over 7 years
    Where in the tar docs does it explain that it is supported? How do I know which commands support it?
  • codeforester
    codeforester over 6 years
    If a command has a special meaning for -, you can find its explanation in the man page.
  • atongsa
    atongsa over 5 years
    su - #make the shell a login shell
  • tripleee
    tripleee over 4 years
    Do not overestimate people's ability to discern manners of speech from literal expressions. This has not been going on for millennia.
  • David R Tribble
    David R Tribble over 4 years
    @tripleee - True, but it has been going on for billions of seconds. ;-)
  • Admin
    Admin almost 4 years
    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.
  • Kenmore
    Kenmore over 3 years
    @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".
  • paxdiablo
    paxdiablo over 3 years
    @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.