Is `-` used only with cd?

cd - can switch between current dir and previous dir.

It seems that I have seen - used as arguments to other commands before, though I don’t remember if - means the same as with cd.

I found that - doesn’t work with ls.

Is - used only with cd?

Asked By: Tim


cd - is actually shorthand for cd "$OLDPWD" && pwd, where $OLDPWD is set each time you change directories to the directory you were just in.

Handling of - depends on the application. Some applications use - to signify STDIN, e.g. grep, awk

Other applications may use - as a shorthand for anything they choose, as Michael’s answer specifies, with su, - is shorthand for --login

Answered By: Lawrence

Any program can use - as an argument, to mean whatever they want. One common example is su, which uses - as shorthand for --login. The only convention I can think of is that programs that read from files frequently use - to mean “read from stdin”, but it’s entirely up to the program

Answered By: Michael Mrozek

Though Michael mentions that su and other applications can use - to mean whatever they want (read from stdin is common), Git does use - in a fashion similar to how cd does, for changing branches.

$ git status
On branch master

$ git checkout foobar
$ git status
On branch foobar

$ git checkout -
$ git status
On branch master
Answered By: dotancohen

- is defined in POSIX Utility Syntax Guidelines as standard input:

Guideline 13:
For utilities that use operands to represent files to be opened for either 
reading or writing, the '-' operand should be used to mean only standard input 
(or standard output when it is clear from context that an output file is being 
specified) or a file named -.

You can see this definition for utilities which operate with files for reading or writing. cd does not belong to these utilities, so - in cd does not follow this guideline.

Besides, POSIX also defined - has own meaning with cd:

    When a <hyphen> is used as the operand, this shall be equivalent to the 

    cd "$OLDPWD" && pwd

    which changes to the previous working directory and then writes its name.
Answered By: cuonglm

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.

Adding my two cents here. I use ‘-‘ to tell the ps2pdf tool to read from standard input, as several people have suggested previously:

man -t ls | ps2pdf - ls.pdf

The above one-liner creates a pdf version of the man page for ls in the current directory.
Hope someone finds it useful :).

Answered By: gacanepa

As answered by gnouc, - has its own meaning in cd.

However, you can replicate the behavior of - in other programs using ~-.
For example:

cd /etc
cd /
ls ~-             #lists the contents of the /etc directory
vim ~-/fstab      #opens the file /etc/fstab in vim
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.