Tcl DISCUSSION

Tcl braces, double quotes and square brackets: when is a variable substituted and when is it not?

Started by syedakalam Tcl quoting rulesbraces vs quotescommand substitutionvariable substitutionTcl syntax
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Tcl braces, double quotes and square brackets: when is a variable substituted and when is it not?

syedakalam Tcl Forum
#1

I am learning Tcl to automate a test bench. puts "Value is $x" prints the value, puts {Value is $x} prints the text $x literally, and if {$x > 5} {...} uses braces but still sees the value. A loop I wrote as while "$i < 10" { incr i } never terminates.

What are the actual rules for braces, quotes and brackets, and why does if behave differently from puts with the same braces?

Community replies 4

Re: Tcl braces, double quotes and square brackets: when is a variable substituted and when is it not?

#2

Tcl has very little syntax. A command is a list of words separated by spaces, and the interpreter makes one pass over each word performing three substitutions: $name is replaced by the variable's value, [command] by that command's result, and backslash sequences by their characters. Double quotes group text containing spaces into one word and still allow all three substitutions. Braces group text into one word and switch substitution off completely: what is between them is passed on exactly as written.

Square brackets are not a grouping device; they run a command and insert its result, as in set n [llength $items].

Re: Tcl braces, double quotes and square brackets: when is a variable substituted and when is it not?

#3

if and puts do not treat braces differently; the interpreter treats both the same, and the commands do different things with the text they receive. puts prints its argument. if receives the unsubstituted string $x > 5 and passes it to the expression evaluator, which looks up $x itself. The body is likewise handed over as plain text and evaluated as a script only if the condition is true.

That delay is the reason for the braces, and it explains your loop. In while "$i < 10" {...} the quotes substitute once, before while even starts, so with i equal to 0 the command receives the constant expression 0 < 10 and tests that on every iteration. With while {$i < 10} the expression is re-read each time round.

Re: Tcl braces, double quotes and square brackets: when is a variable substituted and when is it not?

#4

A difference from Unix shells that is worth knowing: substitution happens once and the result is never re-scanned or split. After set f "my design.v", the command file delete $f passes one argument containing a space; no extra quoting is required. Words are decided before substitution, not after.

When a variable name is followed by characters that could belong to it, delimit the name with braces: "${name}_tb.v". Without them Tcl looks for a variable called name_tb.

Re: Tcl braces, double quotes and square brackets: when is a variable substituted and when is it not?

#5

Layout matters because a newline ends a command. The opening brace of a body must be on the same line as the command it belongs to, and else must follow the closing brace on the same line, in the form if {$x > 5} { ... } else { ... }. Putting the opening brace on the next line in C style makes if fail with a wrong-arguments error, because it sees no body on its own line.

Comments are another trap. # starts a comment only where a command could start, so write set x 5 ;# note with a semicolon for a trailing comment. And braces inside comments still count: an unmatched { in a comment inside a procedure body unbalances the whole body.

TEP COMMUNITY