1
votes

I just did the following experiment in TCL 8.6:

% expr \"\{" ne \"x\"
1

% expr \"\[" ne \"x\"
extra characters after close-quote
in expression ""[" ne "x""

The first command makes sense to me:

  1. Because the argument is not braced, first round parsing is script level parsing, backslash escapes are removed: expr "{" ne "x"
  2. expr command continues the parsing, "{" and "x" are 2 quoted literals and the execution goes well.

The error in the 2nd command does not make sense. The only difference is replacing bracket with brace, why does it fail?

I know bracing the arguments is expected for expression, this question is mostly to understand TCL parsing.

1
The way that commands are parsed, the Tcl interp just looks for string arguments to pass to the expr command. Your string argument \"[" looks like it starts a string after the [ character but that string is not terminated with a closing double quote.MoDJ

1 Answers

2
votes

The problem with the second command is that the expr command processes [] sequences inside double quotes as command substitutions. This is independent of whether Tcl does and is part of why it is a really good idea to always brace your overall expressions. Had you instead used:

expr \{\[\} ne \"x\"

then it would have worked; just as with the base Tcl language, expr does not expand command substitutions in brace-quoted terms.