From: gyro on
On Nov 28, 4:15 pm, "Chris F.A. Johnson" <cfajohn...(a)gmail.com> wrote:
> On 2009-11-28, gyro wrote:
> > Hi,
> > 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.
>
>    The stack is contained in the DIRSTACK array.
>
> ## save DIRSTACK
> printf "%s\n" "${DIRSTACK[@]}" > ~/.dirstack
>
> ## restore DIRSTACK
> DIRSTACK=( $( < ~/.dirstack ) )
>
> ## restore DIRSTACK (bash4)
> mapfile -t DIRSTACK < ~/.dirstack
>
> --
>    Chris F.A. Johnson, author       <http://shell.cfajohnson.com/>
>    ===================================================================
>    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
>    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
>    ===== My code in this post, if any, assumes the POSIX locale  =====
>    ===== and is released under the GNU General Public Licence    =====


Hi Chris,
This looks like a great solution, but unfortunately, the statement
DIRSTACK=( $( < ~/.dirstack ) )
does not appear to load the file entries into the DIRSTACK array for
me.

Below are some details.

Thanks for your help.

-g

=====

OS: Mac OS X (v10.6.2)

$ uname -rsv
Darwin 10.2.0 Darwin Kernel Version 10.2.0

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)

$ cat ~/.dirstack
/Applications
/opt/local/bin
/opt/local
/Library
/Volumes/Storage_Vol
/Library

$ dirs -c

$ cd /sw/etc

$ cd --
0 /sw/etc
1 ~

$ cd /opt/local

$ cd --
0 /opt/local
1 /sw/etc
2 ~

$ dirs -c

$ cd --
0 ~

$ DIRSTACK=( $( < ~/.dirstack ) )
$ cd --
0 ~

$ echo ${DIRSTACK[@]}
~
From: Dave Gibson on
gyro <gyromagnetic(a)gmail.com> wrote:
> On Nov 28, 4:15�pm, "Chris F.A. Johnson" <cfajohn...(a)gmail.com> wrote:
>> On 2009-11-28, gyro wrote:
>> > Hi,
>> > 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.
>>
>> � �The stack is contained in the DIRSTACK array.
>>
>> ## save DIRSTACK
>> printf "%s\n" "${DIRSTACK[@]}" > ~/.dirstack
>>
>> ## restore DIRSTACK
>> DIRSTACK=( $( < ~/.dirstack ) )

> This looks like a great solution, but unfortunately, the statement
> DIRSTACK=( $( < ~/.dirstack ) )
> does not appear to load the file entries into the DIRSTACK array for
> me.

Direct assignment can only modify existing members. To add and remove
directories you'll have to use pushd and popd.
From: mop2 on
On Sat, 28 Nov 2009 19:54:55 -0200, gyro <gyromagnetic(a)gmail.com> wrote:

> On Nov 28, 1:17 pm, Kaz Kylheku <kkylh...(a)gmail.com> wrote:
>> On 2009-11-28, gyro <gyromagne...(a)gmail.com> wrote:
>>
>> > Hi,
>> > 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.
>>
>> You can show the stack with ``dirs''. This enables you to save it to a file.
>> With dirs -p you can print one entry per line, which is more convenient
>> (not to mention robust against spaces in path names).
>>
>> I don't see a way to re-construct the stack directly from such a printed
>> representation, but, obviously, what you can do is push the contents
>> onto the stack one by one.
>>
>> You have to do it in reverse order, though. The last line in the
>> dirs -p output is the bottom of the stack, rather than the first.
>>
>> An easy way to do that is to push onto the stack in the wrong order,
>> then capture the output of ``dirs -p'' again, which reverses it for you.
>> Then clear the stack with dirs -c, and re-push the captured directories.
>>
>> I.e. something like
>>
>> # untested
>> # assumes directory stack has been saved in ~/.bash_dirstack
>>
>> dirs -c
>> while read directory ; do
>> pushd "$directory"
>> done < ~/.bash_dirstack
>>
>> dirs -p > ~/.bash_dirstack.tmp.$$
>>
>> dirs -c
>> while read directory ; do
>> pushd "$directory"
>> done < ~/.bash_dirstack.tmp.$$
>>
>> rm -f ~/.bash_dirstack.tmp.$$
>
> Thanks, Kaz.
> This looks interesting.
>
> -g
>

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
From: Kaz Kylheku on
On 2009-11-28, gyro <gyromagnetic(a)gmail.com> wrote:
> Thanks, Kaz.
> This looks interesting.

I didn't RTFM about the DIRSTACK array variable though.
From: mop2 on
On Mon, 30 Nov 2009 01:34:36 -0200, Kaz Kylheku <kkylheku(a)gmail.com> wrote:

> On 2009-11-28, gyro <gyromagnetic(a)gmail.com> wrote:
>> Thanks, Kaz.
>> This looks interesting.
>
> I didn't RTFM about the DIRSTACK array variable though.
>

But "man bash" says, I think:
DIRSTACK is a special array and can't be easily populated