What is the magic "-" (dash) in command line options?

Examples:

  • Create an ISO image and burn it directly on the CD.

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

  • Go to the previous directory.

    cd -

  • Listen to port 12345 and the data sent to it.

    nc -l -p 12345 | tar xvzf -

What is the purpose of the dash and how to use it?

+83
command-line linux unix bash shell
Nov 08 2018-11-11T00:
source share
6 answers

If you mean naked - at the end of the tar command is common to many commands that want to use the file.

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

This is the case for your first and third example. For example, the cdrecord command accepts standard input (the ISO image stream created by mkisofs ), and writes it directly to /dev/dvdrw .

With the cd every time you change a directory, it saves the directory from which you came. If you run cd with a special "directory name", it uses this remembered directory instead of the real one. You can easily switch between the two directories pretty quickly using this.

Other teams may consider - as another special meaning.

+93
Nov 08 2018-11-11T00:
source share

This is not magic. Some commands interpret - as a user who wants to read from stdin or write to stdout; there is nothing special about the shell.

+15
Nov 08 2018-11-11T00:
source share

Redirecting from or to standard stdin or standard stdout .

See: http://tldp.org/LDP/abs/html/special-chars.html#DASHREF2

+11
Nov 08 '11 at 3:10
source share

Magic is in agreement. For millennia, people have used "-" to distinguish between options and arguments, and have used "-" in the file name to mean either stdin or stdout, if necessary. Do not underestimate the power of the convention!

+6
Nov 08 2018-11-11T00:
source share

- means what every team wants to keep in mind. There are several general conventions, and you saw examples of most of them in other answers, but none of them is 100% universal.

There is nothing magical about the character - about the shell (except that the shell itself and some of its built-in commands, such as cd and echo , use it in the usual ways), Some characters, such as \ , ' and " , are" magic ", with special meanings, wherever they appear. These are" shell metacharacters. " - Not so.

To find out how this command - used - read the documentation for this command.

+6
Nov 08 '11 at 5:23
source share

This means using a standard program input stream.

In the case of cd this means something else: switch to the previous working directory.

+4
Nov 08 2018-11-11T00:
source share



All Articles