2

I've been experimenting with pushd and popd in bash (Cygwin, man page says "GNU bash 4.1") and the man page doesn't quite seem to match the behavior.

For pushd, the command

% pushd +1

both rotates the stack and changes the working directory;

% pushd -n +1

just rotates the stack. This seems to match the man page.

But

% popd +1 

removes an entry from the stack but does NOT change the working directory (even though "-n" was not supplied). This does not seem to match the man page, quoted below:

Arguments, if supplied, have the following meanings:
    -n   Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
    +n   Removes the nth entry counting from the left of the list shown by dirs, starting with zero.  For example: “popd +0” removes the first directory, “popd +1” the second.

Is this the way it is supposed to be?

1 Answer 1

4

I now experiment with popd/pushd and I think that this is how it is supposed to be. The logic is that -n suppresses the change of the directory if it should have been without -n.

popd +N with N not equal to zero does not change the directory. It only removes the element from the stack. So in this situation -n has to do nothing because it has no change to suppress.

On the contrary, if you use popd -n or popd -n +0, the second top element of the stack is removed, rather than that at the very top. Because if you removed the top one, your directory would change. I mean if you have:

 0  ~/one/two/three
 1  ~/one/two
 2  ~/one
 3  ~

when you do popd +0 or popd without -n

your current directory is changed to ~/one/two and the dirs stack is now:

 1  ~/one/two
 2  ~/one
 3  ~

So -n should suppress this behavior. And thus, if, instead you run popd -n +0 or popd -n you are still in your old directory and the dirs stack is now:

 0  ~/one/two/three
 1  ~/one
 2  ~

You can see that the second top element ~/one/two was removed, instead of the very top (so that you could stay in your old directory).

So what I try to highlight here is that the top of your dirs stack is always your current directory. And -n should prevent the change of the directory. So -n should prevent the change of the top of the stack. But if the top of the stack is not changed, the flag -n has nothing to do as it is.

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.