The environmental variable in $PWD has the current working directory. Is there a variable that contains the previous directory? If there is not, how might one be created?

I think this could be useful.


Yes, there is. man bash will lead you to

   OLDPWD The previous working directory as set by the cd command.
  • 1
    Nice, I didn't know about this. – kos Sep 13 '15 at 2:50
  • 1
    This is going to be so useful. (uncd command, anyone?) – Kaz Wolfe Sep 13 '15 at 6:26
  • 11
    You can use cd - to go back to the previous directory. – NZD Sep 13 '15 at 6:43
  • 2
    Bash maintains a stack of all directories you have visited. You can use pushd and popd to manipulate this stack. See man bash for details. – NZD Sep 13 '15 at 6:46
  • 1
    @NZD unless I've somehow been very confused for a long time, the stack does not contain all directories you've visited - only those you've specifically stored using pushd. – David Z Sep 13 '15 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.