bug-bash
[Top][All Lists]
Advanced

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

Re: IFS=':' set -- aa:bb:cc:dd # Fails to set "$@"


From: Greg Wooledge
Subject: Re: IFS=':' set -- aa:bb:cc:dd # Fails to set "$@"
Date: Wed, 1 Dec 2010 12:46:31 -0500
User-agent: Mutt/1.4.2.3i

On Wed, Dec 01, 2010 at 08:14:49AM -0500, steveo@syslang.net wrote:
>       IFS=':' set -- aa:bb:cc:dd

>       Instead, I have to say something like:
>       oldIFS="$IFS"
>       IFS=':'
>       set -- aa:bb:cc:dd
>       IFS="$oldIFS"

Neither one of these sets $1 to aa, $2 to bb and so on.  They both set
$1 to aa:bb:cc:dd.  IFS is not applied to a constant string, ever.  It
is only applied to word-splitting that happens on various substitutions
(and lines of input handled by `read').

You probably fooled yourself by failing to quote $1 when you expanded it:

imadev:~$ IFS=:
imadev:~$ set -- aa:bb:cc:dd
imadev:~$ echo $1
aa bb cc dd
imadev:~$ echo "$1"
aa:bb:cc:dd

IFS is applied to the unquoted $1.

Now, let's pretend you asked a totally different question:

  "I am trying to do this:

    x=aa:bb:cc:dd
    IFS=: set -- $x

  Why doesn't IFS cause $x to be split into four words here?"

Here, IFS is not applied because the substitution of $x is performed
*before* the temporary environment is set up with IFS=: in it.
See http://mywiki.wooledge.org/BashFAQ/104 for a more wordy explanation.



reply via email to

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