Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am a happy user of the cd - command to go to the previous directory. At the same time I like pushd . and popd.

However, when I want to remember the current working directory by means of pushd ., I lose the possibility to go to the previous directory by cd -. (As pushd . also performs cd .).

How can I use pushd to still be able to use cd -

By the way: GNU bash, version 4.1.7(1)

share|improve this question
Why not use pwd to figure out where you are? – Patrick Feb 21 '12 at 12:39
I don't understand your question? The point is that pushd breaks the behavior of cd - that I want (or expect). I know perfectly well in which directory I am, but I want to increase the speed with which I change directories :) – Bernhard Feb 21 '12 at 12:46
You waid "when I want to remember the current working directory by means of pushd .". If you want to remember the current working directory, thats what pwd is for. – Patrick Feb 21 '12 at 13:49
Do you know zsh? It has really nice features like AUTO_PUSHD. – jofel Feb 21 '12 at 14:39
+1 Thank you for teaching me about cd -! For most of a decade, I've been doing $ cd $OLDPWD instead. – Theodore R. Smith Feb 21 '12 at 16:26
up vote 7 down vote accepted

You can use something like this:

push() { 
    if [ "$1" = . ]; then
        builtin pushd .
        cd "$old"
        cd "$current"
        builtin pushd "$1"

If you name it pushd, then it will have precedence over the built-in as functions are evaluated before built-ins.

You need variables old and current as overwriting OLDPWD will make it lose its special meaning.

share|improve this answer
This works perfectly for me. Is there no such feature in the built-in pushd? As I would always prefer a standard solution. Thanks for this function however, maybe I will leave out the argument and it's checking at some point. – Bernhard Feb 21 '12 at 12:41
There is no such feature in the builtin. Your own function is the best solution because pushd and popd both call cd modifying $OLDPWD, hence the source of your problem. I would name the function saved and use it in the context you like too, that of saving cwd. – bsd Feb 21 '12 at 12:53
You might also want to unset old and current after you're done with them. – Wildcard Mar 29 at 23:08

A slightly more concise version of Wojtek's answer:

pushd () {
        if [ "$1" = . ]; then
                cd -
                builtin pushd -
                builtin pushd "$1"

By naming the function pushd, you can use pushd as normal, you don't need to remember to use the function name.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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