The Form of a Tcl Script

A Tcl script is basically this line repeated again and again with different commands and arguments

command ARG1 ARG2 ... ARGn
The arguments are separated by white space, that is, blanks or tabs. For example,
 
set X 2
has two arguments. Not all white space separates arguments. Here are the exceptions.

set X "Hellow orld"
Quotes can surround an argument that contains white space.
set Y {Hellow orld}
Curly brackets can surround an argument that contains white space.
set Z Hellow\ orld
A backslash prevents a white space character from separating arguments.

In all three cases, the second argument consists of the 11 characters "Hellow orld." The effect of the command is to assign this argument to the variable named in the first argument.

Any given command may require a fixed number of arguments or may accept a variable number of arguments.

The command line pattern shown above applies not just to simple commands but also to control structures. Examples:

Since the first word in a Tcl command line can be a procedure or a built-in command, I will use the words "command" and "procedure" almost interchangeably.

Tcl is interpreted. The interpretor

  1. reads a line and performs some substitutions

  2. executes the procedure named by the command

  3. moves on to the next line
Empty lines are ignored. Lines beginning with # are ignored – they serve as comments. The possible substitutions are described below in Variables in Tcl, Command Substitution, and More about Substitution.

Here is a weird example to make the point.

  X + 1
This would be the invocation of a procedure named X with two arguments – if it is on a line by itself. In other contexts, it might be either

  1. three single-character strings passed as arguments to some other procedure or

  2. one five-character string.
As you will see later, it would never be an algebraic expression. Something close to it could be an algebraic expression if passed as a single-string argument to the command expr.

More Observations about Command Lines

Skip these on first reading if you like.

It is possible to put multiple commands on one line of text if the commands are separated by semicolons.

Because # only signals a comment if it appears in place of a command name, comments usually appear on lines by themselves. Because semicolons signal the beginning of a new command, you can put a comment at the end of a command line by using ;# as a prefix for it.

Tcl works with lists a lot. Compared to a language like Lisp, the concept "list" in Tcl is somewhat murky but you will have a good idea of what is meant if you think of a command line as an example of a list. The first element of this list is the command name and subsequent elements are the arguments.

When using Tcl interactively, if a command requires arguments, you can execute a command without any arguments to see a short explanation of how it should be used. For example,

% set
wrong # args: should be "set varName ?newValue?"

Some commands are really families of actions where the action is determined by the first argument. (See below in Action Families.) If you invoke such a command with an inappropriate first argument, you will get a list of the possibilities. For example,

% info ?
bad option "?": should be args, body, cmdcount, commands, complete, default, 
exists, globals, level, library, locals, patchlevel, procs, script, tclversion, 
or vars
% info tclversion
8.0

From Tcl/Tk For Programmers Next section All sections Author J. A. Zimmer Copyright Notice

Jun 17, 1998