Skip to content
VLSI Mentor

Tcl Part I · Module 2 · The Tcl Execution Model

Backslash Substitution and Escaping

The backslash does two separate jobs in Tcl, and it helps to keep them apart. It makes a character that would be special into an ordinary one, which is how you put a dollar sign or a bracket into your text. And at the end of a line it joins that line to the next, which is how one long command is spread over several lines. This chapter teaches both, plus the small set of escape sequences worth knowing by heart — and one piece of behaviour that is the single exception to everything the next two chapters say about braces.

Foundation11 min readTclPart IExecution ModelSubstitutionEscaping

Part I · Module 2 · Chapter 2.6 · Backslash Substitution and Escaping

1. The Problem

You have now met two characters that Tcl treats specially: $ starts a variable reference, and [ starts a command. Quotes and braces are special too.

So how do you put one of those characters in your text, as itself?

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
puts "it costs $5"

Chapter 2.4 §6 showed this fails, because $5 is read as a variable name. You need a way to say "this dollar sign is just a dollar sign".

That is the backslash.

2. Job One: Make a Character Ordinary

A backslash before a character tells Tcl to treat that character as data, not as syntax:

Azvya Education Pvt. Ltd.VLSI Mentor
escaping.tcl
puts "it costs \$5"
puts "She said \"hello\""
puts "a literal \[ and \]"
puts "a backslash: \\"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
it costs $5
She said "hello"
a literal [ and ]
a backslash: \

Each backslash removed the special meaning of exactly one character that followed it. The backslash itself does not appear in the output — it was an instruction, and like quotes and brackets, instructions do not survive into the value.

Note the last line: to get one backslash in your text, you write two.

3. Job Two: Produce a Character You Cannot Type

A few backslash sequences do not escape anything. They stand for characters that are awkward to write directly:

Azvya Education Pvt. Ltd.VLSI Mentor
sequences.tcl
puts "first\nsecond"
puts "col1\tcol2"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
first
second
col1	col2

\n is a newline and \t is a tab. These are the two you will use constantly.

4. Job Three: Continue a Command Onto the Next Line

A newline normally ends a command. A backslash at the very end of a line cancels that, joining the line to the next one:

Azvya Education Pvt. Ltd.VLSI Mentor
continuation.tcl
set items [list one \
                two \
                three]

puts "[llength $items] items: $items"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
3 items: one two three

Tcl read that as one command spread over three lines. The backslash-newline became a single space, so the words stayed separate.

This is how long commands are kept readable. Two rules make it work:

  • The backslash must be the last character on the line. A space after it breaks it, and the error you get will point somewhere confusing.
  • The next line's leading spaces collapse into the join. You can indent the continuation to line things up.

5. Escaping Inside and Outside Quotes

Backslash substitution happens in double-quoted words and in bare words alike:

Azvya Education Pvt. Ltd.VLSI Mentor
both.tcl
puts "quoted: \$5"
puts bare:\ \$5
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
quoted: $5
bare: $5

The second line is unusual-looking Tcl, and it shows something real: in a bare word, \ escapes a space, so the space stops ending the word. You will rarely write this, but it explains why a stray backslash in a bare word changes word boundaries.

In practice: group with quotes and escape inside them. That is the readable form.

6. The One Thing Braces Do Not Stop

The next two chapters say that braces pass text through unchanged. That is almost true, and this is the exception — so it belongs here, where backslashes do.

Braces stop variable substitution and command substitution. They do not stop backslash-newline continuation:

Azvya Education Pvt. Ltd.VLSI Mentor
brace-continuation.tcl
set braced {line one \
line two}

puts ">>$braced<<"
puts "length: [string length $braced]"
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
>>line one  line two<<
length: 18

The backslash and the newline are gone from the value. They were replaced by a space, exactly as they would be outside braces.

So the accurate statement is: braces stop the substitutions that insert values — $ and [ ] — and leave everything else alone, except that a backslash at the end of a line still joins the lines.

7. Common Mistakes

Unescaped dollar signs in text. "$5" is an error. §2.

One backslash where you need two. To print C:\temp, write "C:\\temp". A single backslash before t is a tab.

A space after a line-continuation backslash. The backslash must be last on the line. This is invisible in most editors and is worth checking first when a multi-line command fails.

Escaping inside braces and expecting it to be removed. Braces leave \$ as two characters. If you want escaping applied, use quotes.

Reaching for a table. You need six sequences. §3.

8. Exercises

Exercise 1 — produce exact text. Write one puts for each of these outputs:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
Total: $100
He said "stop"
a [bracket] in text
path: C:\reports

Exercise 2 — two ways. Produce the text [not a command] twice: once using backslashes, once using braces. Which would you use in real code, and why?

Exercise 3 — break a continuation. Take the continuation.tcl example from §4 and put a single space after one of the backslashes. Run it and read the error. Then remove the space and confirm it works. This is worth doing once so you recognise it later.

Exercise 4 — predict the length. Before running, say how many characters each value holds:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
puts [string length "a\tb"]
puts [string length {a\tb}]

Explain the difference using §6.

9. Remember

  • A backslash makes the next character ordinary. That is how $, [, " and \ get into your text.
  • The backslash itself does not appear in the value. It is an instruction.
  • Six sequences cover beginner use: \n, \t, \", \$, \[, \\.
  • A backslash at the end of a line joins it to the next, and becomes a single space.
  • It must be the last character on the line — a trailing space breaks it.
  • Braces stop $ and [ ] substitution, but not backslash-newline. That is the one exception to the next chapter's rule.

Next: the first of the two grouping forms, and the one that still lets substitutions through.

Where this fits

Part of the Tcl curriculum.