3

When using popd, how to push the current directory onto the stack?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
user176549
  • 31
  • 1
  • 2

3 Answers3

1
$ pwd; pushd /tmp; pwd; popd; pwd
/home/users/foo
/tmp ~
/tmp
~
/home/users/foo

Bash will keep a history of the directories you visit, you just have to ask. Bash stores the history in a stack and uses the commands pushd and popd to manage the stack.

If you don't need multiple levels of directory history, you can also do:

cd foo
# do your stuff in foo
cd -

Compared to pushd/popd, this has the disadvantage that if cd foo fails, you end up in the wrong directory with cd -.

(Probably cd - is more handy outside scripts. "Let's go back where I just was.")

See Use pushd and popd to manipulate directory stack for more help.

Rahul
  • 13,309
  • 3
  • 43
  • 54
  • 1
    I also advise the OP to google around for the concept of stack, and the push and pop operations in programming languages. – Rui F Ribeiro Jun 24 '16 at 08:03
1

It should be just

[$]> pushd .

no?

Xiong Chiamiov
  • 704
  • 4
  • 13
0
pushd -n $(pwd)

adds the current directory $(pwd) to the stack without changing directory.

From help pushd in bash:

Options:

-n Suppresses the normal change of directory when adding directories to the stack, so only the stack is manipulated.

cas
  • 1
  • 7
  • 119
  • 185