Using popd without removing last value
How can I get the behavior of using popd
without actually popping the last directory off of the stack? In other words, I want to navigate back to the last directory that I set with pushd
, but I want it to stay at the top of the stack so the next popd
will take me to that same directory (which would still be on the top of the stack) again.
pushd with no arguments swaps the top two entries on the stack, allowing you to effectively cd back and forth between them.
Starting out in d1, execute pushd d2 adds d1 and d2 to the stack and leaves you in d2. Execute pushd again with no arguments, and you're back in d1 with d1 and d2 reversed on the stack.
If I understood correctly, you could pushd .
just after popd
so the pop
ed directory will be placed again at the top of the stack.
How about: cd "$(dirs -l -p | sed -n '2{p;q}')"