2

I often need to go to the directory I was when I logged out of a server. I habitually use cd - for this purpose. However, after logging out and logging back in this does not work because apparently OLDPWD gets reset:

$ cd -
-bash: cd: OLDPWD not set

Is there a way to preserve the value of OLDPWD beyond a login session in bash?

2
  • Possible duplicate of Login via ssh to last used folder
    – Archemar
    Commented Oct 23, 2015 at 7:08
  • When you have favorite directories you want to visit often, you can add an alias for each dir in your .bashrc: alias cdx='cd $HOME/a/very/deep/dir/xantippe' alias cdlog='cd /var/mytool/log' alias cdtest='cd $HOME/test'
    – Walter A
    Commented Oct 25, 2015 at 22:07

1 Answer 1

8

for me, it seems like the easiest approach would be the creation of a .oldpwd file that stores the last pwd location between sessions.

For this you can use the ~/.bash_logout (if using BASH) or ~/.logout (if using tcsh/csh).

Just put some lines like this in the ~/.logout (or ~/.bash_logout)

echo $(pwd) > ~/.oldpwd

and something like this in the ~/.profile (or ~./bash_profile)

export OLDPWD=$(cat ~/.oldpwd)
3
  • 2
    great idea; more simply, have .bash_logout do env | grep OLDPWD > ~/.oldpwd, and have .bash_profile do . ~/.oldpwd
    – Jeff Schaller
    Commented Oct 23, 2015 at 1:23
  • There you , I'm glad the idea helped you! Commented Oct 23, 2015 at 1:55
  • Setting PWD or OLDPWD triggers unspecified behavior. A shell could emit pink smoke instead of what you indent.
    – schily
    Commented Oct 23, 2015 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .