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