From 64254a667a6ba4b553b12369cee2190e5ee0daf8 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Sat, 24 Oct 2009 21:20:04 +0000 Subject: [PATCH] sh: Exempt $@ and $* from set -u This seems more useful and will likely be in the next POSIX standard. Also document more precisely in the man page what set -u does (note that $@, $* and $! are the only special parameters that can ever be unset, all the others are always set, although they may be empty). --- bin/sh/expand.c | 2 +- bin/sh/sh.1 | 7 +++++-- tools/regression/bin/sh/expansion/set-u2.0 | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tools/regression/bin/sh/expansion/set-u2.0 diff --git a/bin/sh/expand.c b/bin/sh/expand.c index 700fa0a9f910..af79e8cad0da 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -657,7 +657,7 @@ again: /* jump here after setting a variable with ${var=text} */ } varlen = 0; startloc = expdest - stackblock(); - if (!set && uflag) { + if (!set && uflag && *var != '@' && *var != '*') { switch (subtype) { case VSNORMAL: case VSTRIMLEFT: diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index 937fda754340..db872fd03eaf 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -32,7 +32,7 @@ .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 .\" $FreeBSD$ .\" -.Dd May 31, 2009 +.Dd October 24, 2009 .Dt SH 1 .Os .Sh NAME @@ -296,7 +296,10 @@ sh -T -c "trap 'exit 1' 2 ; some-blocking-program" .Ed .It Fl u Li nounset Write a message to standard error when attempting -to expand a variable that is not set, and if the +to expand a variable, a positional parameter or +the special parameter +.Va \&! +that is not set, and if the shell is not interactive, exit immediately. .It Fl V Li vi Enable the built-in diff --git a/tools/regression/bin/sh/expansion/set-u2.0 b/tools/regression/bin/sh/expansion/set-u2.0 new file mode 100644 index 000000000000..f81aa62cb6ba --- /dev/null +++ b/tools/regression/bin/sh/expansion/set-u2.0 @@ -0,0 +1,12 @@ +# $FreeBSD$ + +set -u +: $* $@ "$@" "$*" +set -- x +: $* $@ "$@" "$*" +shift $# +: $* $@ "$@" "$*" +set -- y +set -- +: $* $@ "$@" "$*" +exit 0