my_question
my_question

Reputation: 3235

TCL expression parsing - why brace and bracket are escaped differently

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.

Upvotes: 1

Views: 188

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

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.

Upvotes: 2

Related Questions