Hi!

I recently came across a shell script that uses idiom

  var1=var1
  var2=var2
  echo "${var1+($var2)}"

ksh(1) doesn't like it:

  ksh: ${var1+($var2)}": bad substitution

Meanwhile bash and dash just print:

  (var2)

Apparently ksh tries to parse parenthesis within substituted word.
According to 2.2.3 Double-Quotes[1] of POSIX Shell Command Language,
parentheses should not be parsed within double quotes, although 2.6.2
Parameter Expansion[2] sets special rules for parameter substitution
within double quotes, and those don't mention whether substituted word
should be considered quoted or not.

Patch below changes ksh's behavior to match that of bash and dash.  I am
not decided on the matter.

-- 
Dmitrij D. Czarkoff

[1] 
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
[2] 
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02

Index: lex.c
===================================================================
RCS file: /cvs/src/bin/ksh/lex.c,v
retrieving revision 1.64
diff -u -p -r1.64 lex.c
--- lex.c       18 Nov 2015 15:31:21 -0000      1.64
+++ lex.c       16 Dec 2015 07:05:42 -0000
@@ -579,6 +579,15 @@ yylex(int cf)
                        break;
 
                case SBRACEQ:
+                       /*{*/
+                       if (c == '}') {
+                               POP_STATE();
+                               *wp++ = CSUBST;
+                               *wp++ = /*{*/ '}';
+                       } else
+                               goto Subst;
+                       break;
+
                case SBRACE:
                        /*{*/
                        if (c == '}') {

Reply via email to