Thanks Dan - The plot thickens - Yes, you're right, I had $IFS mistakenly set to ':' in the shell in which I ran 'count_args' . Without this IFS setting, I get a count of 4: $ env -i PATH=/bin:/usr/bin HOME=${HOME} /bin/bash --norc $ count_args 1 2 3\ 4 4 $ IFS=: count_args 1 2 3\ 4 3 This to me is strange , as I've asked bash not to use ' ' as a delimiter, when $IFS==: , but it is doing so ! And shouldn't '3\ 4' be a single string in any case, regardless of IFS ? If word splitting is not doing any escaping, why not - shouldn't it be doing so? Escaping works in filenames, so why not in word-splitting ? Thanks & Regards, Jason
On Sun, Jul 29, 2012 at 4:19 PM, Dan Douglas <orm...@gmail.com> wrote: > On Sunday, July 29, 2012 03:23:29 PM Jason Vas Dias wrote: >> echo $(count_args 1 2 3\ 4) > > I should also have mentioned that I couldn't reproduce this case. You should > be getting 4 here in your example, not 3. I have the same Bash version. Are > you sure you were echoing `${#v[@]} ' and not `${#@}', and also that you did > not set IFS=: for count_args? If you use exactly the function you sent with > the default IFS then you should get 4 here. > -- > Dan Douglas RE: >why must non-standard $IFS members be treated so differently ? >Jason Vas Dias >3:23 PM (4 hours ago) > >Good day Chet, list - > I'm concerned about the difference in output of these functions with >the example input > given on the '$' prefixed line below (with 4.2.29(2)-release > (x86_64-unknown-linux-gnu)): > > function count_args { v=($@); echo ${#v[@]}; } > > function count_colons { IFS=':' ; v=($@); echo ${#v[@]}; } > > $ echo $(count_args 1 2 3\ 4) $(count_colons 1:2:3\:4) > 3 4 > It appears to be impossible for an item delimited by 'X' to contain > an escaped 'X' ('\X') if 'X' is not > a standard delimiter (' ', '<TAB>') . Quoting doesn't seem to help either: > > $ echo $(count_args 1 2 3\ 4) $(count_colons 1:2:3':4') > 3 4 > >To me, this appears to be a bug. > >But I bet you're going to tell me it is a feature ? >Please explain. > >Thanks & Regards, >Jason > >BTW, documentation on $IFS does not appear to mention this issue: > > Word Splitting > The shell scans the results of parameter expansion, command > substitution, and arithmetic expansion that did not occur within > double quotes for word splitting. > > The shell treats each character of IFS as a delimiter, and > splits the results of the other expansions into words on these > characters. If IFS is unset, or its value is exactly > <space><tab><newline>, the default, then sequences of <space>, > <tab>, and <newline> at the beginning and end of the results of the > previous expansions are ignored, and any sequence of IFS charac- > ters not at the beginning or end serves to delimit words. If > IFS has a value other than the default, then sequences of the > whitespace characters space and tab are ignored at the > beginning and end of the word, as long as the whitespace character is > in the value of IFS (an IFS whitespace character). Any > character in IFS that is not IFS whitespace, along with any adjacent > IFS whitespace characters, delimits a field. A sequence of IFS > whitespace characters is also treated as a delimiter. If the > value of IFS is null, no word splitting occurs.