From: mop2 on
On Sun, 29 Nov 2009 22:06:25 -0200, mop2 <invalid(a)mail.address> wrote:

>>> On 2009-11-28, gyro <gyromagne...(a)gmail.com> wrote:
>>> > Does anyone know how to save and restore the directory stack in bash?
>>> > I would like to be able to access and manipulate past directories
>>> > between logins.

>
> if you have "tac" (coreutils), you can try:
> #logout
> dirs -p > ~/.dirs`
> #login, if DIRs have no spaces
> dirs -c;for d in `tac ~/.dirs`;do pushd $d;done
>

Possibly the above suggestion does not have the exact desired behavior.


Playing a bit with dirstack (it can be useful to me some day :).

This uses only shell builtins and can be done in just one line for
logout and one for login, in the correspondent scripts, but I see a
source file as more practical way:

$ echo -----;cat /tmp/dirstack;echo -----
-----
case "$1" in
-w)
#logout (~/.bash_logout)
x=0
while dirs -$[x++]
do :
done 2>/dev/null >~/.dirs
unset x
;;
-r)
#login (/etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile)
a=
while read d;do
if [ $a ]
then
pushd $d
else
cd $d
dirs -c
a=1
fi
done<~/.dirs>/dev/null
unset a d
esac

# For debug purposes
{ printf "`pwd`: ";dirs;} >/tmp/dirs$1
#[ "" = -r ]&&
diff /tmp/dirs-w /tmp/dirs-r >/dev/null && echo dirstack OK
-----


#Testing it:
$ ${0//-/} --version|head -n1
GNU bash, version 4.0.35(1)-release (i686-pc-linux-gnu)
$ dirs -c
$ pushd /usr/share >/dev/null
$ pushd /usr/man >/dev/null
$ pushd /usr/include >/dev/null
$ pushd /usr/lib >/dev/null
$ pushd /etc >/dev/null
$ pushd /opt >/dev/null
$ pwd
/opt
$ dirs
/opt /etc /usr/lib /usr/include /usr/man /usr/share /tmp
$ . /tmp/dirstack -w
$ exit

#Opened new xterm:
$ pwd
/tmp
$ dirs
/tmp
$ . /tmp/dirstack -r
dirstack OK
$ pwd
/opt
$ dirs
/opt /etc /usr/lib /usr/include /usr/man /usr/share /tmp
$

Here now, seems to me exactly the same condition as before the last "exit" command.