Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is dash symbol in Linux?

Press Option+Shift+hyphen . Linux desktop environment. Enable the Compose key (instructions for doing that vary depending on your flavor of Linux—for examples, see Linux Keyboard Shortcuts For Text Symbols). After the Compose key is enabled, you can create an em dash by typing the Compose key followed by three hyphens.

What are parameters in commands?

Parameters can be entered in any order. If a parameter has an associated argument, the argument must always follow the parameter. A parameter must start with a '-'; otherwise, it is assumed to be an argument. The maximum length of any single parameter that can be entered into the CLI is 128 bytes.

What does hyphen mean in Unix?

One hyphen is the way options to a command traditionally has been given to Unix-commands. They have one hypen followed by a single letter (or sometimes number) ( -a -i -T ). Some options are followed by an argument ( -ofilename -o filename ).


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.


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.


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


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.


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!