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.
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.
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