20

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.

1 Answer 1

34

Yes, there is. man bash will lead you to

   OLDPWD The previous working directory as set by the cd command.
7
  • 1
    Nice, I didn't know about this.
    – kos
    Sep 13, 2015 at 2:50
  • 1
    This is going to be so useful. (uncd command, anyone?)
    – Kaz Wolfe
    Sep 13, 2015 at 6:26
  • 12
    You can use cd - to go back to the previous directory.
    – NZD
    Sep 13, 2015 at 6:43
  • 3
    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, 2015 at 6:46
  • 2
    @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, 2015 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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