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