help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] The difference between $* and "$*"?


From: Bob Proulx
Subject: Re: [Help-bash] The difference between $* and "$*"?
Date: Sat, 1 Feb 2014 13:52:02 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Peng Yu wrote:
>        *      Expands to the positional parameters, starting from  one.   When
>               the  expansion occurs within double quotes, it expands to a sin-
>               gle word with the value of each parameter separated by the first
>               character of the IFS special variable.  That is, "$*" is equiva-
>               lent to "$1c$2c...", where c is the first character of the value
>               of  the IFS variable.  If IFS is unset, the parameters are sepa-
>               rated by spaces.  If IFS is  null,  the  parameters  are  joined
>               without intervening separators.
> 
> I'm trying to understand the different between $* and "$*". The
> following shows that they behave differently when IFS is not the
> default. But the above paragraph from the manual does not explain
> this.

But it does.  It says:

  That is, "$*" is equivalent to "$1c$2c...", where c is the first
  character of the value of the IFS variable.

So if IFS=":", not the default, then "$*" expands to be $1:$2:$3..."
using the first character from the IFS.

  If IFS is unset, the parameters are separated by spaces.

So if "unset IFS" then "$*" expands to "$1 $2 $3".

  If IFS is null, the parameters are joined without intervening
  separators.

So if "unset IFS" then "$*" expands to "$1$2$3".

> Could anybody let me know if this is documented somewhere else
> in the manual and how to understand the difference? Thanks.

What would you say in the above that would be make it more clear but
still remain concise?

> ~/linux/test/bash/man/variable/IFS/$*$ cat main.sh
> #!/usr/bin/env bash
> 
> set -- a b c d
> echo "$*"

IFS is the default space, tab, newline.  Therefore the result will be
separated by spaces, the first character in the IFS.  "a b c d"

> OLDIFS="$IFS"
> IFS=:
> echo $*

  When the expansion occurs within double quotes, it expands to a
  single word with the value of each parameter separated by the first
  character of the IFS special variable.

Is the $* within double quotes?  No.  So the above does NOT apply.
Instead only this part applies.

  Expands to the positional parameters, starting from  one.

The echo command will receive four arguments.  What does echo do with
those four arguments?  Elsewhere in the manual for the built-in echo part.

       echo [-neE] [arg ...]
              Output the args, separated by spaces, followed by a newline.

Therefore the result is "a b c d".

> echo "$*"

Yes, within double quotes.  Within double quotes expands using the
first character of the IFS, which is now ':'.  Result "a:b:c:d".

> IFS="$OLDIFS"

It is the end of the script so no need to restore the old IFS value
because the script is simply going to exit and the process will
evaporate at that point.

Does it all make sense now?

Bob



reply via email to

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