I'm looking at a script for a pushd function in my bash book:
DIRSTACK=""
export DIRSTACK
pushd ()
{
dirname=$1
DIRSTACK="$dirname ${DIRSTACK:-$PWD' '}"
cd ${dirname:?"missing directory name."}
echo "$DIRSTACK"
}
Wanted to ask if someone could explain what's gong on. I've got most of it, but it's the second line that gets me. When a directory name is entered after the function and it's valid, what exactly is happening? I mean, why the two expressions?
Is it as simple as the function adding the just entered directory (assuming it's valid) to the stack, so that you have
just entered directory/original directory
which, the first time you do this, would evaluate to
just entered directory/pwd
and then the next valid directory would be
second valid directory/previously entered valid directory/pwd
and so on, correct?
so if you enter echo $DIRSTACK you'd get a list of all the entered directories.
But what if you enter a directory that doesn't exist? The book says that it cd fails, but still pushes the bad directory on to the stack. I tried that and I don't think that's what happens (I'm using Darwin (BSD) on my iMac, with Snow Leopard 6.3
if I enter an invalid directory I just get the value of PWD, but not the bad directory that the book says is supposed to be pushed on to DIRSTACK, only a message that says 'No such file or directory'
the book is "Learning the bash shell, third edition" and was written in 2005, covering bash 3.0. Is there something different about bash 3.2, which is on my Mac?