For your information: every time you change directory, whether in an interactive shell or a script, the shell sets the value of the PWD variable to the current directory, and the value of OLDPWD to the previous directory.
Well, usually. As @WilliamPursell pointed out, OLDPWD is not standard, so it might not be available in all shells.
1
Kent
On
try this:
dir="$PWD"
or
dir="$(pwd)"
you may want to have double quotes too if your path contained special chars, like spaces.
The current directory is already in a variable, called
PWD
, and it is automatically set by the shell:You could also:
Or you could use these in your script without storing in additional variables:
For your information: every time you change directory, whether in an interactive shell or a script, the shell sets the value of the
PWD
variable to the current directory, and the value ofOLDPWD
to the previous directory.Well, usually. As @WilliamPursell pointed out,
OLDPWD
is not standard, so it might not be available in all shells.