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