sh: Fix ""$@, which should not use the special case for "$@".

"$@" should expand to no words if there are no positional parameters, but
""$@ should always expand to at least an empty word.
This commit is contained in:
Jilles Tjoelker 2015-11-18 21:09:03 +00:00
parent 1522652230
commit a5cb58abc8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=291025
2 changed files with 33 additions and 1 deletions

View File

@ -249,7 +249,8 @@ argstr(char *p, int flag)
case CTLQUOTEMARK:
lit_quoted = 1;
/* "$@" syntax adherence hack */
if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
if (p[0] == CTLVAR && (p[1] & VSQUOTE) != 0 &&
p[2] == '@' && p[3] == '=')
break;
if ((flag & EXP_FULL) != 0)
USTPUTC(c, expdest);

View File

@ -0,0 +1,31 @@
# $FreeBSD$
failures=''
ok=''
testcase() {
code="$1"
expected="$2"
oIFS="$IFS"
eval "$code"
IFS='|'
result="$#|$*"
IFS="$oIFS"
if [ "x$result" = "x$expected" ]; then
ok=x$ok
else
failures=x$failures
echo "For $code, expected $expected actual $result"
fi
}
testcase 'shift $#; set -- ""$*' '1|'
testcase 'shift $#; set -- $*""' '1|'
testcase 'shift $#; set -- ""$@' '1|'
testcase 'shift $#; set -- $@""' '1|'
testcase 'shift $#; set -- """$*"' '1|'
testcase 'shift $#; set -- "$*"""' '1|'
testcase 'shift $#; set -- """$@"' '1|'
testcase 'shift $#; set -- "$@"""' '1|'
test "x$failures" = x