gaz...@shell.xmission.com (Kenny McCormack):
[…]
> $ IFS=A;set -- aAbAc;echo $#
> 1
> $ echo $1
> a b c
> $
>
> Shouldn't $# be 3?
Field splitting does not happen at the time the command line is
parsed. It takes place only when unquoted shell parameters are
evaluated (“$1” in that example).
The command line
“IFS=A; set -- aAbAc”
sets the positional parameters to be just one parameter having got
the value “aAbAc”. Therefore the command line
“echo "$#"”
yields 1.
In the command line
“echo $1”,
field splitting takes place on the value (“aAbAc”) of the unquoted
parameter expression “$1”, when it comes to constructing the
invocation arguments list of the “echo” utility. Therefore the
“echo” utility is given the four invocation arguments “echo”, “a”,
“b”, and “c”.
> And shouldn't $1 be either "a" or "aAbAc"? Where did the spaces
> come from?
The spaces come from the fact, that “echo” outputs all of its
invocation arguments (except for the first) glued together with
spaces in between. Using the “bash” builtin “printf” rather than
“echo”, try the command lines
“printf '%q\n' "$1"”
and
“printf '%q\n' $1”.
The “bash” builtin “printf” format specifier “%q” tells “printf”
to output the corresponding parameter in a “bash”‐conformant
syntax that could be given to the “eval” builtin to get its value
back. It is also an excellent way to print any byte sequence in
an unambigous way not only for “bash” but also for human readers.
> $ echo "$1"
> aAbAc
> $
Because “"$1"” is a quoted rather than an unquoted reference to
the value of the first positional parameter, there will be no
field splitting, thus “echo” will be invoked with the two
invocation arguments “echo” and “aAbAc”.