tech-userlevel archive

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

Re: Proposal to add /usr/bin/shquote



On Sat, Sep 06, 2008 at 01:40:38PM +0200, Alan Barrett wrote:
> 
> The underlying problem is that the shell doesn't have a "list of
> strings" data type, except in the special case of "$@". Scripts that
> want lists of strings have to simulate them somehow by storing the
> entire list in a single shell variable; most such scripts use space,
> colon, or comma, as separators within the list variable, but they fail
> if the individual items themselves contain that separator.

Ah... In that case you need something like:

save_args() {
    local a c n
    c=0; n=$1; shift
    for a; do
        c=$(($c+1))
        eval $n=\"\$$n \\\"\\\$${n}_\$c\\\"\"
        eval ${n}_$c=\"\$a\"
    done
}
# use:
save_args list -foo bar "arg with spaces" "don't panic" '"Help!"'
# later:
eval set -- $list

        David

-- 
David Laight: david%l8s.co.uk@localhost


Home | Main Index | Thread Index | Old Index