What exactly are the uses of '-' in bash? I know they can be used for
cd -
# to take you to the old 'present working directory'some stream generating com开发者_如何学运维mand | vim -
# somehow vim gets the text.
My question is what exactly is - in bash? In what other contexts can I use it?
Regards Arun
That depends on the application.
cd -
returns to the last directory you were in.
Often -
stands for stdin
or stdout
. For example:
xmllint -
does not check an XML file but checks the XML on stdin
. Sample:
xmllint - <<EOF
<root/>
EOF
The same is true for cat
:
cat -
reads from stdin
. A last sample where -
stands for stdout
:
wget -O- http://google.com
will receive google.com by HTTP and send it on stdout
.
By the way: That has nothing to do with your shell (e.g. bash
). It's only semantics of the called application.
-
in bash has no meaning as a standalone argument (I would not go as far as to say it it does not have a meaning in shell at all - it's for example used in expansion, e.g. ls [0-9]*
lists all files starting with a digit).
As far as being a standalone parameter value, bash will do absolutely nothing special with it and pass to a command as-is.
What the command does with it is up to each individual program - can be pretty much anything.
There's a commonly used convention that -
argument indicates to a program that the input needs to be read from STDIN instead of a file. Again, this is merely how many programs are coded and technically has nothing to do with bash.
From tldp:
This can be done for instance using a hyphen (-) to indicate that a program should read from a pipe
This explains how your vim example gets its data.
There is no universal rule here.
According to the context it changes
It is pretty much useful when you have something to do repeatedly in two directories. Refer #4 here: http://www.thegeekstuff.com/2008/10/6-awesome-linux-cd-command-hacks-productivity-tip3-for-geeks/
In many places it means STDIN.
精彩评论