The POSIX utility syntax guidelines (specifically #13) specify that for utilities that expect a file name to read from, -
means standard input, and for utilities that expect a file name to write to, -
means standard output. For example, cat somefile -
copies the content of somefile
to its standard output, followed by what it reads on its standard input.
This guideline doesn't apply to the cd
command since it doesn't read or write to a file. cd
does something different: the argument -
means “the previous directory”. The command cd -
is equivalent to cd "$OLDPWD" && pwd
. This behavior is specific to the cd
command, and to directly inspired commands such as pushd
.
Note that -
is an operand, not an option. Only arguments that begin with -
and are not just -
or --
are options. The main implication of being an operand is that --
doesn't affect its special meaning. For example, cd -- -P
changes to a subdirectory called -P
, but cd -- -
is the same as cd -
, it doesn't change to a directory called -
. Similarly, cat -- -
doesn't read from a file called -
but from standard input.
cd
is a builtin, not an external command (nor would it have any way of even working as one).cd -
is thus a shell-level feature. – fluffy Jul 28 '14 at 08:23git checkout -
to switch to the previous branch – wim Jul 28 '14 at 12:37-
used with other builtins in the same way? – Tim Jul 28 '14 at 12:40pushd
, yes. Otherwise, the meaning of "the previous directory" is not a terribly useful default. – fluffy Jul 28 '14 at 19:35