1

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.

Stan
  • 1,367
  • 6
  • 24
  • 40

3 Answers3

4

Edit:

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 -
/Users/stefanl
stefanl@host:~ $ cd -
/Users/stefanl/tmp

# `cd -- -` behaves the same as `cd -`
stefanl@host:~/tmp $ cd -- -
/Users/stefanl
stefanl@@host:~ $ cd -- -
/Users/stefanl/tmp
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
/Users/stefanl/-h
stefanl@host:~/-h $
Stefan Lasiewski
  • 22,949
  • 38
  • 129
  • 184
  • 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 09 '10 at 00: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 09 '10 at 00:49
  • @James : Arguments before the `--` are still interpreted. Arguments after the `--` are ignored. – Stefan Lasiewski Nov 09 '10 at 00: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 09 '10 at 00:52
  • @James, I'm a little confused if you are talking about `cd --` or `cd -- -`. – Stefan Lasiewski Nov 09 '10 at 01:08
  • @Stefan I'm talking about both. If, as you state, `cd --` would cause any following arguments to be ignored, why is the `-` not ignored? Also, I'm not the OP :) – James L Nov 09 '10 at 01:12
  • 1
    @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 09 '10 at 01:18
  • @James: I guess `argument` was the wrong word. An `argument` is anything after the command (option flags, filenames, etc). A command `option` is something which is called with the dash symbols (`-h`). In the case of `cd -- -, the `-` is not a command option, therefore it is passed through to the command. I can't find anything which explains the mechanics well, so hopefully this will make my point clear. – Stefan Lasiewski Nov 09 '10 at 01:19
  • @James :Sorry for mistaking you for the OP! ;) – Stefan Lasiewski Nov 09 '10 at 01:20
3

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.

Sam Halicke
  • 6,122
  • 1
  • 24
  • 35
1

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
/home/james/blah
[james@aladdin blah]$ touch hello
[james@aladdin blah]$ ls -
ls: cannot access -: No such file or directory
[james@aladdin blah]$ ls --
hello
[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.

James L
  • 5,915
  • 1
  • 19
  • 24