1Tcl(n) Tcl Built-In Commands Tcl(n)
2
3
4
5______________________________________________________________________________
6
8 Tcl - Tool Command Language
9
11 Summary of Tcl language syntax.
12_________________________________________________________________
13
15 The following rules define the syntax and semantics of the Tcl lan‐
16 guage:
17
18 [1] Commands.
19 A Tcl script is a string containing one or more commands. Semi-
20 colons and newlines are command separators unless quoted as
21 described below. Close brackets are command terminators during
22 command substitution (see below) unless quoted.
23
24 [2] Evaluation.
25 A command is evaluated in two steps. First, the Tcl interpreter
26 breaks the command into words and performs substitutions as
27 described below. These substitutions are performed in the same
28 way for all commands. The first word is used to locate a com‐
29 mand procedure to carry out the command, then all of the words
30 of the command are passed to the command procedure. The command
31 procedure is free to interpret each of its words in any way it
32 likes, such as an integer, variable name, list, or Tcl script.
33 Different commands interpret their words differently.
34
35 [3] Words.
36 Words of a command are separated by white space (except for new‐
37 lines, which are command separators).
38
39 [4] Double quotes.
40 If the first character of a word is double-quote (“"”) then the
41 word is terminated by the next double-quote character. If semi-
42 colons, close brackets, or white space characters (including
43 newlines) appear between the quotes then they are treated as
44 ordinary characters and included in the word. Command substitu‐
45 tion, variable substitution, and backslash substitution are per‐
46 formed on the characters between the quotes as described below.
47 The double-quotes are not retained as part of the word.
48
49 [5] Argument expansion. │
50 If a word starts with the string “{*}” followed by a non-white‐ │
51 space character, then the leading “{*}” is removed and the rest │
52 of the word is parsed and substituted as any other word. After │
53 substitution, the word is parsed as a list (without command or │
54 variable substitutions; backslash substitutions are performed as │
55 is normal for a list and individual internal words may be sur‐ │
56 rounded by either braces or double-quote characters), and its │
57 words are added to the command being substituted. For instance, │
58 “cmd a {*}{b [c]} d {*}{$e f "g h"}” is equivalent to “cmd a b │
59 {[c]} d {$e} f "g h"”.
60
61 [6] Braces.
62 If the first character of a word is an open brace (“{”) and rule
63 [5] does not apply, then the word is terminated by the matching
64 close brace (“}”). Braces nest within the word: for each addi‐
65 tional open brace there must be an additional close brace (how‐
66 ever, if an open brace or close brace within the word is quoted
67 with a backslash then it is not counted in locating the matching
68 close brace). No substitutions are performed on the characters
69 between the braces except for backslash-newline substitutions
70 described below, nor do semi-colons, newlines, close brackets,
71 or white space receive any special interpretation. The word
72 will consist of exactly the characters between the outer braces,
73 not including the braces themselves.
74
75 [7] Command substitution.
76 If a word contains an open bracket (“[”) then Tcl performs com‐
77 mand substitution. To do this it invokes the Tcl interpreter
78 recursively to process the characters following the open bracket
79 as a Tcl script. The script may contain any number of commands
80 and must be terminated by a close bracket (“]”). The result of
81 the script (i.e. the result of its last command) is substituted
82 into the word in place of the brackets and all of the characters
83 between them. There may be any number of command substitutions
84 in a single word. Command substitution is not performed on
85 words enclosed in braces.
86
87 [8] Variable substitution.
88 If a word contains a dollar-sign (“$”) followed by one of the
89 forms described below, then Tcl performs variable substitution:
90 the dollar-sign and the following characters are replaced in the
91 word by the value of a variable. Variable substitution may take
92 any of the following forms:
93
94 $name Name is the name of a scalar variable; the name
95 is a sequence of one or more characters that are
96 a letter, digit, underscore, or namespace separa‐
97 tors (two or more colons).
98
99 $name(index) Name gives the name of an array variable and
100 index gives the name of an element within that
101 array. Name must contain only letters, digits,
102 underscores, and namespace separators, and may be
103 an empty string. Command substitutions, variable
104 substitutions, and backslash substitutions are
105 performed on the characters of index.
106
107 ${name} Name is the name of a scalar variable. It may
108 contain any characters whatsoever except for
109 close braces.
110
111 There may be any number of variable substitutions in a single
112 word. Variable substitution is not performed on words enclosed
113 in braces.
114
115 [9] Backslash substitution.
116 If a backslash (“\”) appears within a word then backslash sub‐
117 stitution occurs. In all cases but those described below the
118 backslash is dropped and the following character is treated as
119 an ordinary character and included in the word. This allows
120 characters such as double quotes, close brackets, and dollar
121 signs to be included in words without triggering special pro‐
122 cessing. The following table lists the backslash sequences that
123 are handled specially, along with the value that replaces each
124 sequence.
125
126 \a Audible alert (bell) (0x7).
127
128 \b [22mBackspace (0x8).
129
130 \f Form feed (0xc).
131
132 \n [22mNewline (0xa).
133
134 \r [22mCarriage-return (0xd).
135
136 \t [22mTab (0x9).
137
138 \v Vertical tab (0xb).
139
140 \<newline>whiteSpace
141 A single space character replaces the backslash, newline,
142 and all spaces and tabs after the newline. This back‐
143 slash sequence is unique in that it is replaced in a sep‐
144 arate pre-pass before the command is actually parsed.
145 This means that it will be replaced even when it occurs
146 between braces, and the resulting space will be treated
147 as a word separator if it is not in braces or quotes.
148
149 \\ Backslash (“\”).
150
151 \ooo The digits ooo (one, two, or three of them) give an
152 eight-bit octal value for the Unicode character that will
153 be inserted. The upper bits of the Unicode character
154 will be 0.
155
156 \xhh The hexadecimal digits hh give an eight-bit hexadecimal
157 value for the Unicode character that will be inserted.
158 Any number of hexadecimal digits may be present; however,
159 all but the last two are ignored (the result is always a
160 one-byte quantity). The upper bits of the Unicode char‐
161 acter will be 0.
162
163 \uhhhh The hexadecimal digits hhhh (one, two, three, or four of
164 them) give a sixteen-bit hexadecimal value for the Uni‐
165 code character that will be inserted.
166
167 Backslash substitution is not performed on words enclosed in
168 braces, except for backslash-newline as described above.
169
170 [10] Comments.
171 If a hash character (“#”) appears at a point where Tcl is
172 expecting the first character of the first word of a command,
173 then the hash character and the characters that follow it, up
174 through the next newline, are treated as a comment and ignored.
175 The comment character only has significance when it appears at
176 the beginning of a command.
177
178 [11] Order of substitution.
179 Each character is processed exactly once by the Tcl interpreter
180 as part of creating the words of a command. For example, if
181 variable substitution occurs then no further substitutions are
182 performed on the value of the variable; the value is inserted
183 into the word verbatim. If command substitution occurs then the
184 nested command is processed entirely by the recursive call to
185 the Tcl interpreter; no substitutions are performed before mak‐
186 ing the recursive call and no additional substitutions are per‐
187 formed on the result of the nested script.
188
189 Substitutions take place from left to right, and each substitu‐
190 tion is evaluated completely before attempting to evaluate the
191 next. Thus, a sequence like
192 set y [set x 0][incr x][incr x]
193 will always set the variable y to the value, 012.
194
195 [12] Substitution and word boundaries.
196 Substitutions do not affect the word boundaries of a command,
197 except for argument expansion as specified in rule [5]. For
198 example, during variable substitution the entire value of the
199 variable becomes part of a single word, even if the variable's
200 value contains spaces.
201
202
203
204Tcl 8.5 Tcl(n)