Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: protect spaces and/or globs
On 2021-02-10 2:14 p.m., Bart Schaefer wrote:
You probably want (@q-) there, to minimize the number of quoted
things, or (@q+) if you use a lot of unicode characters or control
strings.
Seems to be working fine:
grep_wrapper ()
{
grepargs=()
sstring=
while [[ $# -gt 0 ]] ; do
arg="$1"
shift
grepargs+=( "$arg" )
done
sarray=(grep --color=always -i -- "${grepargs[@]}")
print -sr -- "${(@q)sarray}"
echo "\nwhat should be going to history:"
print -r -- "${(@q)sarray}"
echo "\neval:"
eval "${(@q)sarray}"
}
Test run:
9 /aWorking/Zsh/Source/Wk 3 $ . test; grep_wrapper 'on the current'
'i,2,light edit' 'i,1,old stable'
what should be going to history:
grep --color=always -i -- on\ the\ current i,2,light\ edit i,1,old\ stable
eval:
i,2,light edit:Find any command named 'mtr*' on the current path.
i,1,old stable:Find any command named 'mtr*' on the current path.
Up arrow to recall and then re-execute:
9 /aWorking/Zsh/Source/Wk 3 $ grep --color=always -i -- on\ the\ current
i,2,light\ edit i,1,old\ stable
i,2,light edit:Find any command named 'mtr*' on the current path.
i,1,old stable:Find any command named 'mtr*' on the current path.
--------------------------------------------------------------------------------------------------------------------
So, the single quotes might be protected by double quoting as I had it
before,
or we can use the magic of ' (q) ' to protect the spaces with
backslashes even
though the single quotes themselves are gone. Or, using ' (q-) ':
9 /aWorking/Zsh/Source/Wk 3 $ . test; grep_wrapper 'on the current'
'i,2,light edit' 'i,1,old stable'
what should be going to history:
grep --color=always -i -- 'on the current' 'i,2,light edit' 'i,1,old stable'
eval:
i,2,light edit:Find any command named 'mtr*' on the current path.
i,1,old stable:Find any command named 'mtr*' on the current path.
Up arrow and re-execute:
9 /aWorking/Zsh/Source/Wk 3 $ grep --color=always -i -- 'on the current'
'i,2,light edit' 'i,1,old stable'
i,2,light edit:Find any command named 'mtr*' on the current path.
i,1,old stable:Find any command named 'mtr*' on the current path.
-------------------------------------------------------------------------------------------------------
Seems cleaner, the single quotes are themselves protected or replaced so
that
it not only runs right, it looks right; on recall it is visually
exactly the same. And is it not
intuitive that, since single quotes already protect their string, if you
add protection for the
single quotes themselves, you then have a perfectly protected string?
It can be
passed from one function to the next unchanged. This is the holy grail
for me.
Any gotchas? Or is that robust? Can I use it everywhere? Seems
right. The array
separates the arguments even if the might look like they run together in
a string.
Thanks all.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author