Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I found cd - can switch back and forth between previous and current directory which is a very handy feature. Also found cd -- works too, but not sure what exactly that's for. Is there any reference covers this? Please advise, thanks.

share|improve this question
up vote 4 down vote accepted


I'm not clear if you are talking about cd --, or cd -- -. So, I'll try to answer both questions.

cd -- (with no directory) will act the same as cd (with no directory). You will be returned to your home directory. cd -- will not switch you back to the previous working directory.

stefanl@host:~ $ cd tmp
stefanl@host:~/tmp $ cd --
stefanl@host:~ $ cd --
# Note that I do not go back to the directory ~/tmp
stefanl@host:~ $

See how that behavior is different from cd -. This is also true for cd -- -:

stefanl@host:~ $ cd tmp
stefanl@host:~/tmp $ cd -
stefanl@host:~ $ cd -

# `cd -- -` behaves the same as `cd -`
stefanl@host:~/tmp $ cd -- -
stefanl@@host:~ $ cd -- -
stefanl@host:~/tmp $

Regarding -- for most Unix commands:

The -- will tell cd to ignore any options after cd --. From the Bash Man Page:

Unless otherwise noted, each builtin command documented as accepting options preceded by ‘-’ accepts ‘--’ to signify the end of the options.

Pretend you had a directory called -h (Note that I need to use -- here as well):

$ ls -ld -- -h
drwxr-xr-x  2 stefanl  stefanl  68 Nov  8 16:41 -h

You can't cd to this normally, because the command tries to interpret the -h as an option:

$ cd -h
-bash: cd: -h: invalid option
cd: usage: cd [-L|-P] [dir]

So use the -- to tell cd not to process any more options after the --:

stefanl@host:~ $ cd -- -h
stefanl@host:~/-h $ pwd
stefanl@host:~/-h $
share|improve this answer
Are you sure about -- making it ignore arguments? It doesn't work for rm: [james@aladdin blah]$ ls hello [james@aladdin blah]$ rm -- hello [james@aladdin blah]$ ls [james@aladdin blah]$ – James L Nov 9 '10 at 0:45
Ignore above, now you've filled it in I see what you mean, but it still doesn't work for cd. If you cd -- - it still takes you back to the previous directory, not - – James L Nov 9 '10 at 0:49
@James : Arguments before the -- are still interpreted. Arguments after the -- are ignored. – Stefan Lasiewski Nov 9 '10 at 0:49
That doesn't explain cd -- - being interpreted the same as cd -, unless the - is being passed as a path and then interpreted – James L Nov 9 '10 at 0:52
@James: cd -- - says don't process any options after --. It doesn't say don't process any arguments. So it's, in effect, the same as cd -. – Dennis Williamson Nov 9 '10 at 1:18

Previous directory is also cd ~-

Depends on your shell, but check out the dirs builtin (mine is zsh in this case):

[iluvatar]-[/srv/django]-[1126] % dirs -v
0       /srv/django
1       /srv/django/app
2       /srv/django/app2
3       ~

Then, you can use the cd ~N shortcut to change directories, as well as pushd and popd to change the stack.

share|improve this answer
+1 for mentioning pushd/popd – Matt Nov 9 '10 at 1:30

I don't know the answer to this for sure, but from what I've deduced it seems that the -- is being interpreted as the precursor to an argument. As no argument is passed, it's being ignored and so defaults to cd, which takes you to your home directory.

It seems to happen for a few standard utils:
[james@aladdin blah]$ pwd
[james@aladdin blah]$ touch hello
[james@aladdin blah]$ ls -
ls: cannot access -: No such file or directory
[james@aladdin blah]$ ls --
[james@aladdin blah]$ rm -
rm: cannot remove '-': No such file or directory
[james@aladdin blah]$ rm --
rm: missing operand
Try 'rm --help' for more information.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.