Bash – Difference Between “cd -” and “cd ~-“

bashcd-commanddirectorypushd

The Bash command

cd - 

prints the previously used directory and changes to it.

On the other hand, the Bash command

cd ~-

directly changes to the previously used directory, without echoing anything.

Is that the only difference? What is the use case for each of the commands?

Best Answer

There are two things at play here. First, the - alone is expanded to your previous directory. This is explained in the cd section of man bash (emphasis mine):

An argument of - is converted to $OLDPWD before the directory change is attempted. If a non-empty directory name from CDPATH is used, or if - is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output. The return value is true if the directory was successfully changed; false otherwise.

So, a simple cd - will move you back to your previous directory and print the directory's name out. The other command is documented in the "Tilde Expansion" section:

If the tilde-prefix is a ~+, the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is a ~-, the value of the shell variable OLDPWD, if it is set, is substituted. If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a + or a -, the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs builtin invoked with the tilde-prefix as an argument. If the characters following the tilde in the tilde-prefix consist of a number without a leading + or -, + is assumed.

This might be easier to understand with an example:

$ pwd
/home/terdon
$ cd ~/foo
$ pwd
/home/terdon/foo
$ cd /etc
$ pwd
/etc
$ echo ~        ## prints $HOME
/home/terdon
$ echo ~+       ## prints $PWD
/etc
$ echo ~-       ## prints $OLDPWD
/home/terdon/foo

So, in general, the - means "the previous directory". That's why cd - by itself will move you back to wherever you were.

The main difference is that cd - is specific to the cd builtin. If you try to echo - it will just print a -. The ~- is part of the tilde expansion functionality and behaves similarly to a variable. That's why you can echo ~- and get something meaningful. You can also use it in cd ~- but you could just as well use it in any other command. For example cp ~-/* . which would be equivalent to cp "$OLDPWD"/* .