[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-bash] Unquoted $@ inside [[ ... ]]
From: |
Greg Wooledge |
Subject: |
[Help-bash] Unquoted $@ inside [[ ... ]] |
Date: |
Fri, 18 Apr 2014 16:21:23 -0400 |
User-agent: |
Mutt/1.4.2.3i |
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.)
- [Help-bash] Unquoted $@ inside [[ ... ]],
Greg Wooledge <=