cd -
cd -
will bring you to the previously used directory (if there is one) or it will generate an error.
If a previously used directory exists it will change there updating the value of the current working directory and of the previous one, returning a successful exit status (0
).
Otherwise it will print an error message and it will return an exit status (1
).
Do your own check with cd -; echo $?
.
The exit status will become important when you will use it in a script.
An exit status different from 0 can stop the execution of a whole script (if there is a set -e
) or, worst, can lead to an exit point of the cd -
command, different from the one you were thinking when you wrote the script and to execute commands in the wrong directory: imagine that you start in dir0; after you change to dir1 then you fail to change to dir2. Now you execute a cd -
. You think to be in dir1 but instead you are in dir0... and from here it all in the hands of the Fate.
cd $OLDPWD
(or cd $owd
)
cd
is an internal command of all the shells (all, starting from sh
).
Underdash
and bash
it will set the variable PWD
, OLDPWD
for the present and old working directory. Under csh
and tcsh
it will set instead cwd
and owd
.
So with the command cd $OLDPWD
in bash or cd $owd
in tcsh you will be brought to the old working directory if it exists, or to your home directory if this variable is not set.
The exit code will be always 0
, if you have access to your home directory(1).
pushd newdir ... popd
pushd
adds a directory to the stack and popd
removes one from the stack. An advantage respect cd -
is that you will choose when to come back to the signed directory, and you will not be forced to come back to the last one. Another advantage is that you can pile a number of directories and decide to which to jump. help pushd
and help popd
for the information about those builtin commands.
Notes:
To write cd $NotAlreadySetVariable
is the equivalent to write cd
with no parameters at all that will bring you to your home directory. To be precise it will bring you to the directory inside of $HOME
(for bash,dash...) or $home
(for csh,tcsh...). If this directory doesn't exist or is not accessible you will receive an error. If $HOME
(or $home
) is empty you will remain in the present directory, no error will be generated, and the $OLDPWD
(or $owd
) value will be set to the present directory.
The $OLDPWD
, $owd
variable can be useful when you want to use the previous directory as parameter for a command. E.g. You want to move, all files from the present directory to the old one: mv * $OLDPWD
.
Trivial
To be noted from man bash
that in the definition of PWD is used current working directory(cwd) and not something like present working directory (PWD)...
PWD The logical value of the current working directory.
This is set by the cd command.
OLDPWD The previous logical value of the current working directory.
This is set by the cd command.
HOME The home directory of the current user; the default argument for
the cd builtin command. The value of this variable is also used when
performing tilde expansion.
from man tcsh
cd and pushd interpret `-' as the old working directory (equivalent to the shell variable owd). This is not a substitution at all, but an abbreviation recognized by only those commands. Nonetheless, it too can be prevented by quoting.
$OLDPWD
almacenar (cuando exista) la ruta al antiguo directorio utilizado. Escribircd -
es el equivalente (más rápido) de escribircd $OLDPWD
. Puede darse cuenta de que puede usar más ampliamente$OLDPWD
cuándo desea mover, por ejemplo, algunos archivos del directorio actual al antiguo,mv * $OLDPWD
etc., etc.