My test script:

dirs -p >> work_note.txt

In a stack of 10 directories I only get back the top 1 If I run dirs -p or dirs -v on the command line I get the entire stack.

Any tips or knowledge on what I am missing would be appreciated.

NOTE My primary goal is to create a logout process that writes the DIRSTACK to a file so I can recall what work I was doing in a terminal. I usually have multiple terminals going.

2 Answers 2


The DIRSTACK is a local variable and is not passed into scripts, subshells, child processes, and so forth. I recommend using a function loaded into your .bashrc instead of a script.

  • 1
    The correct term is shell variable Apr 7, 2020 at 1:00
  • I think I just realized that it's probably local to the shell. Thanks for the suggestion.
    – Ken Ingram
    Apr 7, 2020 at 1:52

I used this trick to retrieve my dir stack over every sessions. I used to save it on a file. I am also able to import / export to other terminals

Define this function in your .bashrc

stackd () 
    IFS=$(echo -en "\n\b");
    builtin cd $@ && builtin pushd "$PWD" > /dev/null;
    dirs -l -p >> $HOME/.iostackd/dirstack && sort -u -o $HOME/.iostackd/dirstack $HOME/.iostackd/dirstack > /dev/null;

overload your buitin 'cd' by alias

alias cd='stackd "$1"'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .