Someone in IRC had some code whose behavior I don't really understand.
The code in question was:
if [[ $@ == $','* ]]
At first glance this looks like it's just equivalent to:
if [[ $1 = ,* ]]
But as it turns out, this is not the case.
imadev:~$ foo=' ,foo'
imadev:~$ set -- "$foo"
imadev:~$ if [[ $foo = ,* ]]; then echo yes; else echo no; fi
no
imadev:~$ if [[ $@ = ,* ]]; then echo yes; else echo no; fi
yes
imadev:~$ if [[ $1 = ,* ]]; then echo yes; else echo no; fi
no
imadev:~$ IFS=; if [[ $@ = ,* ]]; then echo yes; else echo no; fi
no
imadev:~$ unset IFS
Somehow, IFS whitespace trimming is being performed on the expansion of
unquoted $@ inside the [[ ]] test. But similar trimming is not performed
when the left hand side is $foo or $1. Is this intended?
(Yes, we told the person in IRC that the code is horrible.)