What does "cd -" mean?

In the bash shell today, I noticed the following command at the end of the script. I know what cd , but I don't know about the meaning of a dash after it.

 cd - 

What does it mean? Google naively trims - so I can't find his answer.

+44
linux bash shell
Mar 16 '12 at 15:41
source share
7 answers

If one dash is specified as an argument, it will be replaced with the OLDPWD value.

OLDPWD installed with the cd , and this is the previous working directory.

+56
Mar 16 '12 at 15:44
source share

cd - returns to the previous directory.

For example:

 marcelo @ marcelo: ~ $ cd / opt
 marcelo @ marcelo: / opt $ cd / usr / bin
 marcelo @ marcelo: / usr / bin $ cd -
 / opt
 marcelo @ marcelo: / opt $ 

I was in / opt, changed to / usr / bin, and then returned / opt with cd -

+11
Mar 16 '12 at 15:43
source share

cd - returns you to the last directory.

 $ cd ~/Desktop $ pwd /Users/daknok/Desktop $ cd / $ pwd / $ cd - $ pwd /Users/daknok/Desktop 
+9
Mar 16 2018-12-12T00:
source share

cd - returns to the previous directory in which you were.

Say I'm in /usr/ and I type cd /var/local/someplace/else

Then I use cd - I will return to /usr

+5
Mar 16 '12 at 15:44
source share

From the manual

The argument of is equivalent to $ OLDPWD. If the non-empty directory is a name from CDPATH, or if it is the first argument, and the directory change is successful, the absolute path of the new working directory is written to standard output. Return The value is true if the directory was successfully changed; false otherwise

Therefore, it - equivalent to $OLDPWD , which contains the last directory in which the shell was located and is installed by the previous call to cd .

+4
Mar 16 '12 at 3:50
source share

From a person found here: http://ss64.com/bash/cd.html

 Quickly get back $ cd - 
0
Mar 16 2018-12-12T00:
source share

cd - returns you to the last directory that you were. eg.

 cd ~/Documents cd ~ cd / 

Now you are in '/', and if you run 'cd -', you will be in '~'. BTW, run 'cd -' again, you will return to '/', but not '~ / Documents'

0
Aug 18 '17 at 14:06 on
source share



All Articles