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: Greg Wooledge
Subject: Re: [Help-bash] The difference between $* and "$*"?
Date: Mon, 3 Feb 2014 08:16:50 -0500
User-agent: Mutt/1.4.2.3i

On Sat, Feb 01, 2014 at 01:52:02PM -0700, Bob Proulx wrote:
>   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".

That last sentence is wrong.  You meant to say, if IFS="" then "$*"
expands to "$1$2$3".


On a separate note, I prefer examples for this kind of answer:

imadev:~$ set -- one "one and a half" two
imadev:~$ args $*
6 args: <one> <one> <and> <a> <half> <two>
imadev:~$ args "$*"
1 args: <one one and a half two>
imadev:~$ ( IFS=""; args "$*" )
1 args: <oneone and a halftwo>
imadev:~$ args "$@"
3 args: <one> <one and a half> <two>


Where args is:

imadev:~$ cat bin/args
#! /bin/sh
printf "%d args:" "$#"
printf " <%s>" "$@"
echo


You almost always want "$@" unless you're joining array elements into
a single string.



reply via email to

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