Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
1  
Do you know zsh? It has really nice features like AUTO_PUSHD. –  jofel Feb 21 '12 at 14:39
1  
+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
show 2 more comments

2 Answers

up vote 3 down vote accepted

You can use something like this:

function push () 
{ 
    if [ "$1" == "." ]; then
        old=$OLDPWD;
        current=$PWD;
        builtin pushd .;
        cd $old;
        cd $current;
    else
        builtin pushd "$1";
    fi
}

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. –  bdowning Feb 21 '12 at 12:53
add comment

A slightly more concise version of Wojtek's answer:

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

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
add comment

Your Answer

 
discard

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.