help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Anomalies with ${*}


From: Eric Blake
Subject: Re: [Help-bash] Anomalies with ${*}
Date: Tue, 27 Mar 2012 07:20:09 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120316 Thunderbird/11.0

On 03/27/2012 07:08 AM, Bill Gradwohl wrote:
> When abc contains 2 spaces, I can replace them and get a null string with
> the pattern substitution.
> When I replace the abc with *, it no longer works as is evidenced by the
> tests.

${*...} is similar to address@hidden - both forms perform the substitution _on
each element_ of the positional parameters individually, then
concatenates those results afterwards.

${*// /} says "strip all spaces from $1, strip all spaces from $2, ...,
then concatenate those results with the first character of $IFS", which
will re-add spaces if $IFS starts with a space (or is unset).

> 
> I have a case where I have from 1 to 3 passed parameters. They could all be
> null strings. What I wanted to do is use * to aggregate all the passed
> parameters, knowing that they would be separated by the 1st char of IFS - a
> space. If all the parms are null strings, this would produce a null string
> for only 1 parm, a single space for 2 parms and 2 spaces for 3 parms. Using
> simple substitution of all spaces to null would tell me that all the parms
> were null.

Why not something simpler? If IFS is null, then $* concatenates all
parameters without any spacing.  Thus:

saveIFS=$IFS
IFS=
test "$*" || syntax
IFS=$saveIFS

is a way to call 'syntax' if all positional parameters were empty.

> 
> Alternatively setting IFS='' when evaluating the ${*} should also produce a
> null string. It doesn't.

How exactly did you test it?  It worked for me in my example above; are
you sure you were setting IFS properly?

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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