The command

CD -

will change the pwd to the last directory.

Is there a way to know which directory it is, so that I'll know where I am CD'ing to?

link|improve this question
feedback

migrated from stackoverflow.com Dec 19 '09 at 2:46

This question came from our site for professional and enthusiast programmers.

3 Answers

Yes, it is $OLDPWD.

~$ cd src/
~/src$ cd ..
~$ echo $OLDPWD
/home/$USER/src
link|improve this answer
Thanks Dirk for the quick solution. – Abhijit Dec 18 '09 at 13:04
Always a pleasure :) – Dirk Eddelbuettel Dec 18 '09 at 13:18
"echo ~-" will output the same information – njd Feb 19 '10 at 15:23
feedback

The previous directory is saved in $OLDPWD

link|improve this answer
feedback

You can also use ~- (anywhere you can use tilde expansion) instead of $OLDPWD:

# Copy a file from the previous working directory.
cp ~-/file1 .

There is also ~+ for $PWD, which is useful for commands which require an absolute directory:

./configure --prefix ~+/root

Note that things like --prefix=~+/root won't work due to the rules of tilde expansion; use --prefix="$PWD/root" instead.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown