Ok this is a short question. I just happened to know that with pushd command, we can add more working directories into our list, which is handy. But is there a way to make this list permanent, so it can survive reboots or logoffs?
-
2Well you can write a little script to be ran at log in to accomplish that.M. Becerra– M. Becerra2017-05-16 10:52:00 +00:00Commented May 16, 2017 at 10:52
1 Answer
You may pre-populate the directory stack in your ~/.bashrc file if you wish:
for dir in "$HOME/dir" /usr/src /usr/local/lib; do
pushd -n "$dir" >/dev/null
end
or, if you want to put the directories in an array and use them from there instead:
dirstack=( "$HOME/dir"
/usr/src
/usr/local/lib )
for dir in "${dirstack[@]}"; do
pushd -n "$dir" >/dev/null
end
unset dirstack
With -n, pushd won't actually change the working directory, but instead just add the given directory to the stack.
If you wish, you can store the value of the DIRSTACK array (upper-case variable name here), which is the current directory stack, into a file from ~/.bash_logout, and then read that file in ~/.bashrc rather than using a predefined array.
In ~/.bash_logout:
declare -p DIRSTACK >"$HOME/.dirstack"
In ~/.bashrc:
if [ -f "$HOME/.dirstack" ]; then
source "$HOME/.dirstack"
fi
I don't know how well this would work in a situation where you use multiple terminals. The .dirstack file would be overwritten every time a terminal exited, if it ran a bash as a login shell.