From 0cbc4905aca5857f60dc856428cd166deaa783b6 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Tue, 12 May 2020 13:06:35 +0800 Subject: [PATCH] Updated SC2155 (markdown) --- SC2155.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/SC2155.md b/SC2155.md index b9d2d28..5a78c0f 100644 --- a/SC2155.md +++ b/SC2155.md @@ -66,15 +66,19 @@ foo="$(mycmd)" readonly foo ``` -#### Word splitting and quoting issue with dash, maybe others +## Word splitting and quoting issue with dash, maybe others A serious quoting problem with dash is another reason to declare and assign separately. Dash is the [default, `/bin/sh` shell on Ubuntu](https://wiki.ubuntu.com/DashAsBinSh). More specifically, dash version 0.5.8-2.10 and others cannot run this code: ```sh -VAR1='white spa/ce' -local var2=$(printf '%s' "${VAR1}") +f(){ local e=$1; } +g(){ local g=$(printf '%s' "foo 2"); } +f "1 2" +g ``` While this runs fine in other shells, [dash doesn't treat this as an assignment](http://mywiki.wooledge.org/BashPitfalls#local_var.3D.24.28cmd.29) and fails like this: ``` -local: spa/ce: bad variable name +local: 2: bad variable name ``` -After separating this runs fine in any shell. If you really don't want to separate it, you can double quote the substitutions. \ No newline at end of file +The direct workaround to this bug is to quote the right-hand-side of the assignment. Separating declaraction and assignment also makes this runs fine in any shell. + +(A rule to catch this problem is in the works at [#1556](https://github.com/koalaman/shellcheck/issues/1556)). \ No newline at end of file