help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Putting escaped elements in an array


From: Jesper Nygårds
Subject: [Help-bash] Putting escaped elements in an array
Date: Sun, 27 Apr 2014 15:35:38 +0200

I wanted to keep an array of directories that I have visited, and for this
array to be automatically updated. I therefore wrote the following:

PROMPT_COMMAND=_push-dir

declare lastdir
declare -a dirstack

_push-dir () {
    local currentdir=$(ls -d --quoting-style=escape "$(readlink -e .)")
    if [[ -n $lastdir && "$lastdir" != "$currentdir" ]]; then
        eval dirstack=( $(printf "%q" "$lastdir") $(printf "%q "
"address@hidden") )
    fi
    lastdir="${currentdir}"
}

Every time a new prompt is created, it checks whether I have changed to a
new directory, and if so adds the old directory to the array (I don't want
to add the current dir until I've left it).

This works fine, but I spent hours getting it to work for directory names
with spaces in it, and the combination above of "eval", "ls
--quoting-style=escape" and "printf "%q"" seems overly complicated to me,
and I was therefore wondering if there's a simpler way to do it?

Note that want to add (and later to recall) directories with spaces in
them, and of course I don't want to lose the escaping when newer items are
added to the already existing ones.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]