I know I can pushd / popd if I want to save the current location before chdir into another directory. In bash I can go back to the directory before the last chdir by:

$ chdir /somedir
$ chdir -

I'm wondering if I can go back for several steps (chdir ---- maybe??), just using chdir rather than pushd / popd, in bash is that possible?


Did you know that pushd saves directories on a stack, so it can have multiple directories saved?

You can skip around using a command such as:

pushd +3; popd
  • 1
    You can even use alias cd=pushd, to avoid having to type the extra characters in pushd. – Chris Johnsen Dec 20 '09 at 6:15
  • Thanks, but pushd prints the annoying stack contents, and, BTW won't it be blown up if pushd too many times? – Xiè Jìléi Jul 10 '10 at 8:29

No, it's impossible.

  • You can always write a shell function wrapper around chdir that checks if the argument is multiple dashes and then calls popd, while dumping the stack contents to /dev/null. – Jaap Eldering Oct 28 '11 at 13:07

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.