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. Secondly, the first word is used to
29 locate a command procedure to carry out the command, then all of
30 the words of the command are passed to the command procedure.
31 The command procedure is free to interpret each of its words in
32 any way it likes, such as an integer, variable name, list, or
33 Tcl script. Different commands interpret their words differ‐
34 ently.
35
36 [3] Words.
37 Words of a command are separated by white space (except for new‐
38 lines, which are command separators).
39
40 [4] Double quotes.
41 If the first character of a word is double-quote (“"”) then the
42 word is terminated by the next double-quote character. If semi-
43 colons, close brackets, or white space characters (including
44 newlines) appear between the quotes then they are treated as
45 ordinary characters and included in the word. Command substitu‐
46 tion, variable substitution, and backslash substitution are per‐
47 formed on the characters between the quotes as described below.
48 The double-quotes are not retained as part of the word.
49
50 [5] Argument expansion.
51 If a word starts with the string “{*}” followed by a non-white‐
52 space character, then the leading “{*}” is removed and the rest
53 of the word is parsed and substituted as any other word. After
54 substitution, the word is parsed as a list (without command or
55 variable substitutions; backslash substitutions are performed as
56 is normal for a list and individual internal words may be sur‐
57 rounded by either braces or double-quote characters), and its
58 words are added to the command being substituted. For instance,
59 “cmd a {*}{b [c]} d {*}{$e f {g h}}” is equivalent to “cmd a b
60 {[c]} d {$e} f {g h}”.
61
62 [6] Braces.
63 If the first character of a word is an open brace (“{”) and rule
64 [5] does not apply, then the word is terminated by the matching
65 close brace (“}”). Braces nest within the word: for each addi‐
66 tional open brace there must be an additional close brace (how‐
67 ever, if an open brace or close brace within the word is quoted
68 with a backslash then it is not counted in locating the matching
69 close brace). No substitutions are performed on the characters
70 between the braces except for backslash-newline substitutions
71 described below, nor do semi-colons, newlines, close brackets,
72 or white space receive any special interpretation. The word
73 will consist of exactly the characters between the outer braces,
74 not including the braces themselves.
75
76 [7] Command substitution.
77 If a word contains an open bracket (“[”) then Tcl performs com‐
78 mand substitution. To do this it invokes the Tcl interpreter
79 recursively to process the characters following the open bracket
80 as a Tcl script. The script may contain any number of commands
81 and must be terminated by a close bracket (“]”). The result of
82 the script (i.e. the result of its last command) is substituted
83 into the word in place of the brackets and all of the characters
84 between them. There may be any number of command substitutions
85 in a single word. Command substitution is not performed on
86 words enclosed in braces.
87
88 [8] Variable substitution.
89 If a word contains a dollar-sign (“$”) followed by one of the
90 forms described below, then Tcl performs variable substitution:
91 the dollar-sign and the following characters are replaced in the
92 word by the value of a variable. Variable substitution may take
93 any of the following forms:
94
95 $name Name is the name of a scalar variable; the name
96 is a sequence of one or more characters that are
97 a letter, digit, underscore, or namespace separa‐
98 tors (two or more colons). Letters and digits
99 are only the standard ASCII ones (0–9, A–Z and
100 a–z).
101
102 $name(index) Name gives the name of an array variable and
103 index gives the name of an element within that
104 array. Name must contain only letters, digits,
105 underscores, and namespace separators, and may be
106 an empty string. Letters and digits are only the
107 standard ASCII ones (0–9, A–Z and a–z). Command
108 substitutions, variable substitutions, and back‐
109 slash substitutions are performed on the charac‐
110 ters of index.
111
112 ${name} Name is the name of a scalar variable or array
113 element. It may contain any characters whatso‐
114 ever except for close braces. It indicates an
115 array element if name is in the form “array‐
116 Name(index)” where arrayName does not contain any
117 open parenthesis characters, “(”, or close brace
118 characters, “}”, and index can be any sequence of
119 characters except for close brace characters. No
120 further substitutions are performed during the
121 parsing of name.
122
123 There may be any number of variable substitutions in a single
124 word. Variable substitution is not performed on words enclosed
125 in braces.
126
127 Note that variables may contain character sequences other than
128 those listed above, but in that case other mechanisms must be
129 used to access them (e.g., via the set command's single-argument
130 form).
131
132 [9] Backslash substitution.
133 If a backslash (“\”) appears within a word then backslash sub‐
134 stitution occurs. In all cases but those described below the
135 backslash is dropped and the following character is treated as
136 an ordinary character and included in the word. This allows
137 characters such as double quotes, close brackets, and dollar
138 signs to be included in words without triggering special pro‐
139 cessing. The following table lists the backslash sequences that
140 are handled specially, along with the value that replaces each
141 sequence.
142
143 \a Audible alert (bell) (Unicode U+000007).
144
145 \b Backspace (Unicode U+000008).
146
147 \f Form feed (Unicode U+00000C).
148
149 \n Newline (Unicode U+00000A).
150
151 \r Carriage-return (Unicode U+00000D).
152
153 \t Tab (Unicode U+000009).
154
155 \v Vertical tab (Unicode U+00000B).
156
157 \<newline>whiteSpace
158 A single space character replaces the backslash, newline,
159 and all spaces and tabs after the newline. This back‐
160 slash sequence is unique in that it is replaced in a sep‐
161 arate pre-pass before the command is actually parsed.
162 This means that it will be replaced even when it occurs
163 between braces, and the resulting space will be treated
164 as a word separator if it is not in braces or quotes.
165
166 \\ Backslash (“\”).
167
168 \ooo The digits ooo (one, two, or three of them) give a eight-
169 bit octal value for the Unicode character that will be
170 inserted, in the range 000–377 (i.e., the range
171 U+000000–U+0000FF). The parser will stop just before
172 this range overflows, or when the maximum of three digits
173 is reached. The upper bits of the Unicode character will
174 be 0.
175
176 \xhh The hexadecimal digits hh (one or two of them) give an
177 eight-bit hexadecimal value for the Unicode character
178 that will be inserted. The upper bits of the Unicode
179 character will be 0 (i.e., the character will be in the
180 range U+000000–U+0000FF).
181
182 \uhhhh The hexadecimal digits hhhh (one, two, three, or four of
183 them) give a sixteen-bit hexadecimal value for the Uni‐
184 code character that will be inserted. The upper bits of
185 the Unicode character will be 0 (i.e., the character will
186 be in the range U+000000–U+00FFFF).
187
188 \Uhhhhhhhh
189 The hexadecimal digits hhhhhhhh (one up to eight of them)
190 give a twenty-one-bit hexadecimal value for the Unicode
191 character that will be inserted, in the range
192 U+000000–U+10FFFF. The parser will stop just before this
193 range overflows, or when the maximum of eight digits is
194 reached. The upper bits of the Unicode character will be
195 0.
196
197 The range U+010000–U+10FFFD is reserved for the future.
198
199 Backslash substitution is not performed on words enclosed in
200 braces, except for backslash-newline as described above.
201
202 [10] Comments.
203 If a hash character (“#”) appears at a point where Tcl is
204 expecting the first character of the first word of a command,
205 then the hash character and the characters that follow it, up
206 through the next newline, are treated as a comment and ignored.
207 The comment character only has significance when it appears at
208 the beginning of a command.
209
210 [11] Order of substitution.
211 Each character is processed exactly once by the Tcl interpreter
212 as part of creating the words of a command. For example, if
213 variable substitution occurs then no further substitutions are
214 performed on the value of the variable; the value is inserted
215 into the word verbatim. If command substitution occurs then the
216 nested command is processed entirely by the recursive call to
217 the Tcl interpreter; no substitutions are performed before mak‐
218 ing the recursive call and no additional substitutions are per‐
219 formed on the result of the nested script.
220
221 Substitutions take place from left to right, and each substitu‐
222 tion is evaluated completely before attempting to evaluate the
223 next. Thus, a sequence like
224
225 set y [set x 0][incr x][incr x]
226
227 will always set the variable y to the value, 012.
228
229 [12] Substitution and word boundaries.
230 Substitutions do not affect the word boundaries of a command,
231 except for argument expansion as specified in rule [5]. For
232 example, during variable substitution the entire value of the
233 variable becomes part of a single word, even if the variable's
234 value contains spaces.
235
237 backslash, command, comment, script, substitution, variable
238
239
240
241Tcl 8.6 Tcl(n)