Prev: Examine standard output and exit immediately if the exit codeis non-zero?
Next: What is the most portable syntax of these two choices?
From: Ben Bacarisse on 27 Jul 2010 07:25 Allodoxaphobia <knock_yourself_out(a)example.net> writes: > On 2010-07-26 in comp.unix.shell, Seebs wrote: <snip> >> What I >> usually use is take advantage of the positional parameters, because they >> have a magic thing: "$@". >> >> Basically, if you have two positional parameters, then "$@" is equivalent >> to "$1" "$2". And that means that you can start doing cool stuff, like: >> >> set -- "$@" -a "A1 A2" <snip> > |$ set -- "$@" -a "A1 A2" > |$ echo "$@" > |-a A1 A2 > > I need <-a "A1 A2"> --- I need the quoted |A1 A2| string. echo is not a good way to see what's happening. Instead, try something like: printf '[%s]\n' "$@" and you'll see [-a] [A1 A2] printf re-uses the format for each argument showing you that you have only two and the second has the space preserved. Another way to get the same magic is to use bash's arrays: "${array[@]}" expands to the elements of the array with each one as a separate word: extras=(-a "A1 A2") printf '|%s|\n' "${extras[@]}" This is not portable between shells. The "$@" method is. <snip> -- Ben.
From: Allodoxaphobia on 27 Jul 2010 10:28
On 27 Jul 2010 02:53:43 GMT, Seebs wrote: > On 2010-07-27, Allodoxaphobia <knock_yourself_out(a)example.net> wrote: >> Ahh, but... My "A1 A2" is a (required-to-be) quoted string containing >> (usually) more than one 'word'. > > Yes. That is preserved. > > What you really need is an argument containing a space. Quotes are just one > way of getting the space there. > >> "and the quoting ... will be preserved." ??? > > Yes. > >> |$ set -- "$@" -a "A1 A2" >> |$ echo "$@" >> |-a A1 A2 >> >> I need <-a "A1 A2"> --- I need the quoted |A1 A2| string. > > You have it. > > The quotes were eaten by the shell when it passed to echo, but you really > did call echo with only two arguments: > -a > A1 A2 > which is what you need. Ahhhhh!! It is now obvious, after your explanation. It's a new day and I've learned something new. Life is good. :-) Jonesy -- Marvin L Jones | jonz | W3DHJ | linux 38.24N 104.55W | @ config.com | Jonesy | OS/2 * Killfiling google & XXXXbanter.com: jonz.net/ng.htm |