#2493 Backslashes inside curly braces

obsolete: 8.4.4
Reinhard Max
Reinhard Max

According to rule 5 of the Tcl syntax the sequence \\}
would have to be treated as one literal backslash
followed by an escaped closing brace when it appears
between curly braces. But instead the parser treats it
as two literal backslashes followed by an unescaped
closing brace. So either the documentation or the
implementation is wrong.


  • Reinhard Max
    Reinhard Max

    • summary: Backslashes at the end of braced words --> Backslashes inside curly braces
  • Don Porter
    Don Porter

    • assigned_to: hobbs --> dgp
  • Don Porter
    Don Porter

    Logged In: YES

    When scanning a brace quoted word,
    backslash substitution is not performed,
    but each backslash sequence is scanned
    as a unit. This detail might not be
    completely obvious from the documentation,
    but it is required in order to only do
    backslash-newline substitution in the
    correct places.

    proc foo {} {
    set backslash \\ }

    We don't want the backslash-newline
    here converted to a space. So the second
    backslash has to be tied to the first as a unit
    during parsing.

    If you have a proposed improvement to the
    docs to make this clearer without making it
    confusing, I'd consider it.

  • Don Porter
    Don Porter

    • assigned_to: dgp --> rmax
    • status: open --> closed
  • Logged In: YES

    If it is just a docs update, I think it is important to note
    there that it is not possible to construct a string that
    ends in a single or any odd number of backslashes. Meaning
    you cannot make a DOS path ending in a backslash in curly
    braces: {c:\somewhere\} doesn't work. Nor does
    {c:\somewhere\\}, which produces c:\somewhere\\

    I know you should not do it this way, but it has bit me before.

  • Don Porter
    Don Porter

    Logged In: YES

    Just to clarify, there's no limit on your ability
    to construct a string in Tcl that ends in an
    odd number of backslashes.

    You just can't brace-quote such a string.
    (Because it would escape the closing } )
    You also can't ""-quote such a string for
    the same reason. So in this corner case,
    you're stuck either using all \-quoting or,
    more likely, using substitution to generate
    the string you need.

    set bs \\ set myString "foo bar$bs"