From: Russell King - ARM Linux Date: Thu, 27 Feb 2014 10:39:24 +0000 Subject: [U-Boot] [PATCH] hush: when a variable expansion is empty, don't drop the parameter In-Reply-To: <1393484106-16642-1-git-send-email-swarren@wwwdotorg.org> References: <1393484106-16642-1-git-send-email-swarren@wwwdotorg.org> Message-ID: <20140227103924.GH21483@n2100.arm.linux.org.uk> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de On Wed, Feb 26, 2014 at 11:55:06PM -0700, Stephen Warren wrote: > The following shell command fails: > > if test -z "$x"; then echo "zero"; else echo "non-zero"; fi > > (assuming $x does not exist, it prints "non-zero" rather than "zero"). > > ... since "$x" expands to nothing, and the argument is completely > dropped, causing too few to be passed to -z, causing cmd_test() to > error out early. > > This is because when variable expansions are processed by make_string(), > the expanded results are concatenated back into a new string. However, > no quoting is applied when doing so, so any empty variables simply don't > generate any parameter when the combined string is parsed again. > > Fix this by explicitly replacing any empty expansion with an empty pair > of quotes, so that an argument is still generated. > > Reported-by: Russell King > Signed-off-by: Stephen Warren > --- > Hmm. I wonder if this causes other problems, such as: > > if test -z $x; then echo "zero"; else echo "non-zero"; fi > > I guess we should only quote the expansion if the input string had quotes > around the text being expanded, but I'm not sure if we can know that. > Perhaps more investigation is needed:-( Although... I /guess/ that case > is rarer, so this patch still improves things? Standard shell will only drop an empty expansion if it's unquoted, so: a="1 2 3" b=4 c= echo $a $b $c ends up with arg1 = "1", arg2 = "2", arg3 = "3", arg4 = "4" and no arg5 echo "$a" "$b" "$c" ends up with arg1 = "1 2 3", arg2 = "4", arg3 = "" and no arg4 echo "$a $b $c" ends up with arg1 = "1 2 3 4 " and no arg2 If you'd like to check out this behaviour, this script will help you see each argument as it was passed by the shell. #!/bin/sh n=1 for a in "$@"; do echo "arg$n: \"$a\"" n=$(($n + 1)) done Getting this right matters, because it affects stuff like the ability to make use of test -z / test -n, while preserving the ability to have unquoted empty variables disappear from argument lists. -- FTTC broadband for 0.8mile line: now at 9.7Mbps down 460kbps up... slowly improving, and getting towards what was expected from it.