What does 'cd -' stand for?
LinuxBashShellLinux Problem Overview
In a bash shell script today I noticed the below command at the end of the script. I know what is cd
but I am unaware of the significance of a dash after it.
cd -
What does this mean? Google naively truncates the -
so I am unable to find its answer.
Linux Solutions
Solution 1 - Linux
If a single dash is specified as the argument, it will be replaced by the value of OLDPWD
.
The OLDPWD
is set by cd
command and it is the previous working directory.
Solution 2 - Linux
cd -
returns to the directory you were previously.
For instance:
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 went back to /opt with cd -
Solution 3 - Linux
cd -
brings you back to the last directory.
$ cd ~/Desktop
$ pwd
/Users/daknok/Desktop
$ cd /
$ pwd
/
$ cd -
$ pwd
/Users/daknok/Desktop
Solution 4 - Linux
cd -
returns to the previous directory you were in.
Say I'm in /usr/
and I type cd /var/local/someplace/else
Then I use cd -
I'll return to /usr
Solution 5 - Linux
From the manual
> An argument of - is equivalent to $OLDPWD. 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
Therefore the -
is equivalent to the $OLDPWD
, which holds the last directory which the shell was in, and is set by the previous cd
invocation.
Solution 6 - Linux
From the man found here : http://ss64.com/bash/cd.html
Quickly get back
$ cd -
Solution 7 - Linux
cd - bring you back to the last directory you were. e.g.
cd ~/Documents
cd ~
cd /
Now you are in '/', and if you run 'cd -' you will be in ''. BTW, run 'cd -' once again, you will return to '/' but not '/Documents'
Solution 8 - Linux
“ Current Directory “ Is what the bash cd terminal command means. It means “ keep me in this directory “