Skip to content
VLSI Mentor

Tcl Part I · Module 2 · The Tcl Execution Model

Double Quotes: Grouping With Substitution

Quotes in Tcl do not do what quotes do in most languages. They do not create a string. They are an instruction to the parser that says: everything from here to the matching quote is one word, and the substitutions you already know still apply inside it. That is a smaller claim than people assume, and getting it right is what makes the next two chapters straightforward instead of confusing.

Foundation10 min readTclPart IExecution ModelGroupingQuotes

Part I · Module 2 · Chapter 2.7 · Double Quotes: Grouping With Substitution

1. Quotes Do Not Make a String

If you have written C or Python, quotes mean "this is a piece of text". A string literal.

In Tcl they mean something narrower:

Everything from here to the matching quote is one word.

That is the whole job. The quotes are an instruction about word boundaries, and like every instruction in Module 2, they do not appear in the value.

2. The Quotes Disappear

Azvya Education Pvt. Ltd.VLSI Mentor
disappear.tcl
puts "hello there"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
hello there

No quotation marks in the output. They never reached puts — they told Tcl where the word ended, and then they were done.

This is easy to accept and easy to forget. If you want quotation marks in the text, you escape them, which is Chapter 2.6:

Azvya Education Pvt. Ltd.VLSI Mentor
literal-quotes.tcl
puts "She said \"stop\""
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
She said "stop"

3. The Grouping Job

Without quotes, a space ends a word:

Azvya Education Pvt. Ltd.VLSI Mentor
grouping.tcl
proc show {args} {
    puts "received [llength $args] word(s)"
    set i 1
    foreach w $args {
        puts "  word $i: $w"
        incr i
    }
}

show my report.txt
show "my report.txt"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
received 2 word(s)
  word 1: my
  word 2: report.txt
received 1 word(s)
  word 1: my report.txt

This is Chapter 2.3 again. The quotes turned two arguments into one.

4. The Substitution Job

Here is what makes quotes different from braces. Inside quotes, all three substitutions still run.

Variables:

Azvya Education Pvt. Ltd.VLSI Mentor
subst-var.tcl
set name parser
puts "running $name"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
running parser

Commands:

Azvya Education Pvt. Ltd.VLSI Mentor
subst-cmd.tcl
puts "length is [string length hello]"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
length is 5

Backslashes:

Azvya Education Pvt. Ltd.VLSI Mentor
subst-bs.tcl
puts "first\nsecond"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
first
second

All three. Quotes group, and then get out of the way.

5. Both Jobs at Once

This is the normal shape of real Tcl output — fixed text and substituted values, grouped into one argument:

Azvya Education Pvt. Ltd.VLSI Mentor
both.tcl
set name parser
set items {a b}

puts "running $name on [llength $items] items"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
running parser on 2 items

Trace it:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
Words:
    word 1:  puts
    word 2:  "running $name on [llength $items] items"

Inside word 2:
    $name             →  parser
    [llength $items]  →  2

Final values:
    word 1:  puts
    word 2:  running parser on 2 items

Run:
    puts "running parser on 2 items"   (one argument)

One argument, built from three pieces. Without the quotes this would be six separate words and puts would reject it.

6. A Quote Only Groups at the Start of a Word

Quotes are special only when a word begins with one. In the middle of a word, a quote is an ordinary character:

Azvya Education Pvt. Ltd.VLSI Mentor
mid-word.tcl
show one"two"three
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
received 1 word(s)
  word 1: one"two"three

One word, with the quotation marks kept. Tcl had already started the word at o, so the quote was just data.

The practical consequence:

Azvya Education Pvt. Ltd.VLSI Mentor
broken.tcl
set y a" b"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
wrong # args: should be "set varName ?newValue?"

That looked like it would set y to a b. Instead the word started at a, the quotes were literal, the space ended the word, and set received three arguments.

Start the word with the quote, or group the whole thing.

7. Common Mistakes

Thinking quotes create a string type. They create one word. Tcl decides what a value means when a command uses it, not when you quote it.

Forgetting to escape an inner quote. "She said "stop"" is not one word. It is three, and you get an argument error.

Expecting quotes to protect $. They do not. "costs $5" is still an error. If you want a literal dollar, escape it — or use braces, the next chapter.

Quoting a bare variable out of habit. "$path" and $path give the same single word. Harmless, but it is a shell reflex, not a Tcl need.

Starting a quote mid-word. §6.

8. Exercises

Exercise 1 — count and content. For each line, predict the word count and the value of the first word:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
show hello world
show "hello world"
show "hello" "world"
show "hello world" again

Exercise 2 — build one message. With set user ada and set count 3, write a single puts that prints exactly:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
ada has 3 items

Then write it again so that the 3 is produced by [string length "abc"] instead of a variable.

Exercise 3 — the literal quote. Write one puts that prints exactly:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
The flag is "-verbose"

Exercise 4 — explain a failure. Predict what set msg hello" "world does before running it. Then run it, read the error, and explain it using §6.

9. Remember

  • Quotes group, they do not create a string. They mark where one word ends.
  • The quotes do not appear in the value. Escape them with \" if you need them in the text.
  • All three substitutions still run inside quotes — $name, [command], and backslashes.
  • This is the normal way to build a message from fixed text and values.
  • A quote only groups when it starts a word. Elsewhere it is an ordinary character.
  • Quotes do not protect $. Use \$, or braces.

Next: the other grouping form, which does the opposite.

Where this fits

Part of the Tcl curriculum.