10.41.2.1 Syntax

A Tcl script consists of a series of strings separated from each other by a newline character. Each string contains a command or series of semi-colon separated commands. A command is a series of words separated by spaces. The first word in a command is the name of the command and subsequent words are its arguments.

An example is:

     set a 1
     set b 2

which is a Tcl script of two commands: the first command sets the value of variable a to 1, and the second command sets the value of variable b to 2.

An example of two commands on the same line separated by a semi-colon is:

     set a 1; set b 2

which is equivalent to the previous example but written entirely on one line.

A command is executed in two phases. In the first phase, the command is broken down into its constituent words and various textual substitutions are performed on those words. In the second phase, the procedure to call is identified from the first word in the command, and the procedure is called with the remaining words as arguments.

There are special syntactic characters that control how the first phase, the substitution phase, is carried out. The three major substitution types are variable substitution, command substitution, and backslash substitution.

Variable substitution happens when a ‘$’ prefixed word is found in a command. There are three types of variable substitution:

An example of variable substitution is:

     set a 1
     set b $a

which sets the value of variable a to 1, and then sets the value of variable b to the value of variable a.

Command substitution happens when a word contains an open square bracket, ‘[’. The string between the open bracket and matching closing bracket are treated as a Tcl script. The script is evaluated and its result is substituted in place of the original command substitution word.

A simple example of command substitution is:

     set a 1
     set b [set a]

which does the same as the previous example but using command substitution. The result of a set a command is to return the value of a, which is then passed as an argument to set b and so variable b acquires the value of variable a.

Backslash substitution is performed whenever the interpreter comes across a backslash. The backslash is an escape character and when it is encountered is causes the interpreter to handle the next characters specially. Commonly escaped characters are ‘\a’ for audible bell, ‘\b’ for backspace, ‘\f’ for form feed, ‘\n’ for newline, ‘\r’ for carriage return, ‘\t’ for horizontal tab, and ‘\v’ for vertical tab. Double-backslash, ‘\\’, is substituted with a single backslash. Other special backslash substitutions have the following forms:

Any other character that is backslash escaped is simply substituted by the character itself. For example, \W is replaced by W.

A further syntactic construction is used to delay substitution. When the beginning of a word starts with a curly bracket, ‘{’, it does not do any of the above substitutions between the opening curly bracket and its matching closing curly bracket. The word ends with the matching closing curly bracket. This construct is used to make the bodies of procedures in which substitutions happen when the procedure is called, not when it is constructed. Or it is used anywhere when the programmer does not want the normal substitutions to happen. For example:

     puts {I have $20}

will print the string ‘I have $20’ and will not try variable substitution on the ‘$20’ part.

A word delineated by curly brackets is replaced with the characters within the brackets without performing the usual substitutions.

A word can begin with a double-quote and end with the matching closing double-quote. Substitutions as detailed above are done on the characters between the quotes, and the result is then substituted for the original word. Typically double-quotes are used to group sequences of characters that contain spaces into a single command word.

For example:

     set name "Fred the Great"
     puts "Hello my name is $name"

outputs ‘Hello my name is Fred the Great’. The first command sets the value of variable name to the following double-quoted string "Fred the Great". The the next command prints its argument, a single argument because it is a word delineated by double-quotes, that has had variable substitution performed on it.

Here is the same example but using curly brackets instead of double-quotes:

     set name {Fred the Great}
     puts {Hello my name is $name}

gives the output ‘Hello my name is $name’ because substitutions are suppressed by the curly bracket notation.

And again the same example but without either curly brackets or double-quotes:

     set name Fred the Great
     puts Hello my name is $name

simply fails because both set and puts expect a single argument but without the word grouping effects of double-quotes or curly brackets they find that they have more than one argument and throw an exception.

Being a simple scripting language, Tcl does not have any real idea of data types. The interpreter simply manipulates strings. The Tcl interpreter is not concerned with whether those strings contain representations of numbers or names or lists. It is up to the commands themselves to interpret the strings that are passed to them as arguments in any manner those choose.


Send feedback on this subject.