Scroll to navigation

Tcl(3tcl) Tcl Built-In Commands Tcl(3tcl)


NAME

Tcl - Tool Command Language

SYNOPSIS

Summary of Tcl language syntax.


DESCRIPTION

The following rules define the syntax and semantics of the Tcl language:

[1] Script.
A script is composed of zero or more commands delimited by semi-colons or newlines.
[2] Command.
A command is composed of zero or more words delimited by whitespace. The replacement for a substitution is included verbatim in the word. For example, a space in the replacement is included in the word rather than becoming a delimiter, and \\ becomes a single backslash in the word. Each word is processed from left to right and each substitution is performed as soon as it is complete. For example, the command

set y [set x 0][incr x][incr x]

is composed of three words, and sets the value of y to 012.

If hash (“#”) is the first character of what would otherwise be the first word of a command, all characters up to the next newline are ignored.

[3] Braced word.
If a word is enclosed in braces (“{”) and (“}”) , the braces are removed and the enclosed characters become the word. No substitutions are performed. Nested pairs of braces may occur within the word. A brace preceded by an odd number of backslashes is not considered part of a pair, and neither brace nor the backslashes are removed from the word.
[4] Quoted word.
If a word is enclosed in double quotes (“"”) , the double quotes are removed and the enclosed characters become the word. Substitutions are performed.
[5] List.
A list has the form of a single command. Newline is whitespace, and semicolon has no special interpretation. There is no script evaluation so there is no argument expansion, variable substitution, or command substitution: Dollar-sign and open bracket have no special interpretation, and what would be argument expansion in a script is invalid in a list.
[6] Argument expansion.
If “{*}” prefixes a word, it is removed. After any remaining enclosing braces or quotes are processed and applicable substitutions performed, the word, which must be a list, is removed from the command, and in its place each word in the list becomes an additional word in the command. For example,
cmd a {*}{b [c]} d {*}{$e f {g h}}
is equivalent to
cmd a b {[c]} d {$e} f {g h} .
[7] Evaluation.
To evaluate a script, an interpreter evaluates each successive command. The first word identifies a procedure, and the remaining words are passed to that procedure for further evaluation. The procedure interprets each argument in its own way, e.g. as an integer, variable name, list, mathematical expression, script, or in some other arbitrary way. The result of the last command is the result of the script.
[8] Command substitution.
Each pair of brackets (“[”) and (“]”) encloses a script and is replaced by the result of that script.
[9] Variable substitution.
Each of the following forms begins with dollar sign (“$”) and is replaced by the value of the identified variable. name names the variable and is composed of ASCII letters (AZ and az), digits (09), underscores, or namespace delimiters (two or more colons). index is the name of an individual variable within an array variable, and may be empty.
$name
name may not be empty.

$name(index)
name may be empty. Substitutions are performed on index.
${name}
name may be empty.
${name(index)}
name may be empty. No substitutions are performed.
Variables that are not accessible through one of the forms above may be accessed through other mechanisms, e.g. the set command.
[10] Backslash substitution.
Each backslash (“\”) that is not part of one of the forms listed below is removed, and the next character is included in the word verbatim, which allows the inclusion of characters that would normally be interpreted, namely whitespace, braces, brackets, double quote, dollar sign, and backslash. The following sequences are replaced as described:
Audible alert (bell) (U+7).
Backspace (U+8).
Form feed (U+C).
Newline (U+A).
Carriage-return (U+D).
Tab (U+9).
Vertical tab (U+B).
\<newline>whiteSpace
Newline preceded by an odd number of backslashes, along with the consecutive spaces and tabs that immediately follow it, is replaced by a single space. Because this happens before the command is split into words, it occurs even within braced words, and if the resulting space may subsequently be treated as a word delimiter.
\\
Backslash (“\”).
Up to three octal digits form an eight-bit value for a Unicode character in the range 0377, i.e. U+0–U+FF. Only the digits that result in a number in this range are consumed.
Up to two hexadecimal digits form an eight-bit value for a Unicode character in the range 0FF.
Up to four hexadecimal digits form a 16-bit value for a Unicode character in the range 0FFFF.
Up to eight hexadecimal digits form a 21-bit value for a Unicode character in the range 010FFFF. Only the digits that result in a number in this range are consumed.

KEYWORDS

backslash, command, comment, script, substitution, variable

8.6 Tcl