Skip to content
VLSI Mentor

Tcl Part I · Module 2 · The Tcl Execution Model

Braces vs Quotes: The Decision Rule

You now know what each form does. This chapter is about choosing, which is a different skill and the one that actually costs people time. The two forms are not styles — they are different instructions, and picking the wrong one usually does not produce an error. It produces the wrong text, or a body that was substituted too early, and those are much harder to notice than a crash. One question settles almost every case, and the rest of this chapter is the handful of situations where it needs a second thought.

Foundation11 min readTclPart IExecution ModelGroupingQuoting

Part I · Module 2 · Chapter 2.9 · Braces vs Quotes: The Decision Rule

1. The Same Characters, Two Results

Azvya Education Pvt. Ltd.VLSI Mentor
compare.tcl
set n 5

puts "n is $n"
puts {n is $n}
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
n is 5
n is $n

Both lines group six characters into one word. Both are valid. Neither errors. They simply mean different things.

Two parallel left-to-right flows from the same source text. The quoted form allows substitution and produces the value n is 5. The braced form performs no substitution and produces the literal text n is dollar n.Quoted"n is $n"Substitution runs$n becomes 5Word valuen is 5Braced{n is $n}Nothing runscharacters pass throughWord valuen is $n12
Figure 1 — the same source text through both forms. Quotes let the substitution run, so the word becomes the value. Braces pass the characters through, so the word is the text as written. Both produce exactly one word; what differs is what is inside it.

2. Why This Bug Is Expensive

Most mistakes in Tcl produce an error. This one usually does not.

Azvya Education Pvt. Ltd.VLSI Mentor
silent.tcl
set user ada
puts {Welcome, $user}
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
Welcome, $user

No error. The script runs, exits zero, and prints something wrong. If that line were writing a report or a filename rather than a greeting, the mistake would travel.

That is why this chapter exists as its own chapter. The cost is not difficulty, it is silence.

3. The Rule

One question:

Do I want the value now, or the text exactly as written?

  • Value now → double quotes.
  • Text as written → braces.

That settles almost every line you will write.

4. The Same Rule as a Table

What you are writingUseWhy
A message containing a variable"..."you want the value in the text
A message containing [command]"..."you want the result in the text
Text containing a literal $ or [{...}no escaping needed
A body of code for if, while, foreach, proc{...}the command runs it later
A condition for if or while{...}the command evaluates it later
An expression for expr{...}Module 3 explains why
A fixed list of values{...}nothing to substitute

The last four rows are all the same reason in different clothes: something else will deal with this text later, so do not touch it now.

5. The Cases the Rule Does Not Settle Immediately

Text with no special characters at all. puts "hello" and puts {hello} are identical in effect. Use quotes — it is the convention, and it leaves room to add a variable later.

A body that genuinely needs an outer value. Occasionally you want part of a body fixed at the time you write it. That is a real need and it has proper answers in Module 10 and Module 11. Reaching for quotes around a body is almost always the wrong one, because it substitutes everything, not the one thing you meant — Chapter 2.8 Exercise 3 shows that failure.

A list of values you want substituted. {$a $b} is two literal words, not two values. If you want the values, build the list with a command rather than by quoting — Module 6 covers it.

6. Reading Someone Else's Code

The rule works backwards too, and this is how you read Tcl quickly.

When you see braces, ask: who runs this later? The answer is usually the command it is being passed to.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
if {$count == 0} {
    puts "nothing to do"
}

Two braced words. The first will be evaluated by if as a condition. The second will be run by if if the condition is true. Neither is if syntax — both are ordinary words, deferred on purpose. That reading is Chapter 2.1 §5 and it is now the normal way to look at Tcl.

7. Common Mistakes

Braces in a message. §2. Wrong text, no error.

Quotes around a body. The body is substituted once, when the line is read, instead of each time it runs.

Quotes around a condition. Same problem. if "$x == 1" {...} evaluates the condition text once at parse time; braces are what let if evaluate it properly.

Switching forms to "fix" an error without knowing why. If changing quotes to braces makes an error go away, find out what changed. Otherwise you have moved the bug.

Believing one is safer. Neither is. They are different instructions.

8. Exercises

Exercise 1 — apply the rule. For each line, say which form is correct and why:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
puts ??? Welcome, $user ???
puts ??? use $5 and [brackets] in text ???
if ??? $count > 0 ??? ??? puts "work to do" ???

Exercise 2 — find the silent bug. This script runs without error and is wrong. Find the line and fix it:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
set run 7
set dir {output_$run}
puts "writing to $dir"

Exercise 3 — both correct. Write two lines that both print hello — one with quotes, one with braces. Then explain when the choice would start to matter.

Exercise 4 — predict four. With set a 2:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
puts "a = $a"
puts {a = $a}
puts "sum = [expr {$a + 1}]"
puts {sum = [expr {$a + 1}]}

Exercise 5 — read it backwards. Look at any foreach or if you have written in this module. For each braced word, say which command will run it and when.

9. Remember

  • Ask one question: do I want the value now, or the text as written? Quotes for the first, braces for the second.
  • Both forms produce exactly one word. The difference is what is inside it.
  • The wrong choice usually produces wrong text, not an error. That is what makes it expensive.
  • Bodies, conditions and expressions are braced because another command deals with them later.
  • Neither form is safer. They are different instructions.
  • Reading braces means asking who runs this later.

One thing is still unexplained: what ends a command in the first place. That is next, and it also explains a brace error that sends people looking in the wrong place.

Where this fits

Part of the Tcl curriculum.