How do I use, i.e. to have in bash variable, a directory just've been pushd, and not to do popd command ?
Asked
Active
Viewed 345 times
2 Answers
0
If you want to know just the name of the previous directory you can issue
% echo $OLDPWD
If you want to go to that directory, you can simply do
% cd -
which will automatically changedir to $OLDPWD
If you want to the full directory stack that you have created using pushd-popd
% dirs -v
this will print the directory index alongside the directory name
You can jump to a directory by just giving
% pushd +N
where N is the index of the directory.
amisax
- 2,957
- 17
- 23
-
1`echo $OLDPWD` will fail if any of the directories in that path contain whitespace. Double-quite the variable when you use it (as you should do for any variable), like `echo "$OLDPWD"` – roaima Apr 19 '21 at 20:26
0
In Bash, there's the DIRSTACK array variable:
DIRSTACK
An array variable containing the current contents of the directory stack. Directories appear in the stack in the order they are displayed by the dirs builtin.
The last one pushed is "${DIRSTACK[0]}", the second at index [1], etc.
me@hostname /tmp$ pushd a
/tmp/a /tmp
me@hostname /tmp/a$ pushd /tmp/b
/tmp/b /tmp/a /tmp
me@hostname /tmp/b$ dirs
/tmp/b /tmp/a /tmp
me@hostname /tmp/b$ declare -p DIRSTACK
declare -a DIRSTACK=([0]="/tmp/b" [1]="/tmp/a" [2]="/tmp")
ilkkachu
- 133,243
- 15
- 236
- 397