help-bash
[Top][All Lists]
Advanced

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

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


From: Greg Wooledge
Subject: Re: [Help-bash] Anomalies with ${*}
Date: Tue, 27 Mar 2012 08:13:27 -0400
User-agent: Mutt/1.4.2.3i

On Mon, Mar 26, 2012 at 10:13:56PM -0600, Bill Gradwohl wrote:
> Sorry for building on my last email, but I found some other anomalous
> behavior with ${*}.

Gah!  Use email theading!  That way we know there's another one....

> abc='  '
> echo "->${abc}<-"
> echo "->${abc// /}<-"

You are not showing the output, and you are not saying what you EXPECTED
the output to be.  What anomaly do you think you are seeing?

> set -- '' '' ''
> echo "->${*}<-"
> echo "->${*// /}<-"

There are no spaces, so there is nothing to substitute.

> IFS='' set -- '' '' ''

That doesn't even make sense!  Set IFS only for the duration of a set
command?

> IFS= set -- '' '' ''

Same.

> IFS=''
> set -- '' '' ''
> echo "->${*}<-"

OK, now that at least isn't nonsense.  You set IFS, and then you set the
positional parameters to a series of empty strings, and then you perform
a substitution.  You should get -><- I believe.  So let's test it.

imadev:~$ IFS=''
imadev:~$ set -- '' '' ''
imadev:~$ echo "->${*}<-"
-><-

Now, and this is VERY important, you must put IFS back or everything
else you do in that shell will be weird, and you will get confused.

imadev:~$ unset IFS

> IFS=
> set -- '' '' ''
> echo "->${*}<-"

That is precisely the same as the one before.  What are you trying to do?

> Every one of the echo's referencing * should be a null string as far as I
> can tell, but some aren't.

What?  "$*" is a substitution that concatenates multiple things together
into a single output string.

> Why does nulling out IFS work as a separate statement, but not on the same
> line?

Because you set IFS *only for the duration of the set command*.  The value
of IFS goes *back to what it was* as soon as the set ends.  So, by the
time you use IFS (e.g. implicitly in the "$*" expansion), it has reverted.

> The reason I'm doing this is to see if all the passed parameters are null
> strings.

allnull=1
for arg; do
  if [[ $arg ]]; then
    allnull=0
    break
  fi
done

Seriously, use a loop.  Do not attempt to write clever, tricky code,
especially if you do not understand the language.  Even if by some
accident you get the tricky code to work, you won't understand it when
you have to maintain it 4 months later (or worse, when someone else has
to maintain it).



reply via email to

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