bug-bash
[Top][All Lists]
Advanced

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

Re: eval a=b\ c


From: Charles Daffern
Subject: Re: eval a=b\ c
Date: Mon, 25 May 2015 22:45:49 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

On 25/05/15 20:33, address@hidden wrote:
>   if [[ -n $ZSH_VERSION ]] ; then
>     echo "[ ${(q)@} ]"
>     eval    ${(q)@}
>   else
>     echo "[ $@ ]"
>     eval    $@
>   fi
I believe the bash equivalent here would be something along the lines of:

quoted=$(printf '%q ' "$@")
quoted=${quoted% }
echo "[ $quoted ]"
eval "$quoted"

You mention that bash is "unable to expand a variable with quoting
intact", but nor is zsh. Zsh and bash can only quote a variable so that
it won't break when used as an argument, but there is no way to retrieve
the original quoting once it has been parsed by the shell.

Usually if you want to pass around executable bash code you should do it
in one already properly-quoted string that gets passed around in a
single argument. Quoting the string (as we're having to do as part of
"echodo") might be better off as another function. You already have zsh
and bash cases, and I've seen some 'clever' shell quoting tricks at
http://www.etalabs.net/sh_tricks.html which should work across pretty
much every bourne-like shell under the sun.




reply via email to

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