MPI für Kohlenforschung

University College London

Tcl Basics

Some Simple Tcl Commands

The following provides a very brief summary of the Tcl language syntax. The user is strongly advised to refer to the man page for Tcl (note the uppercase T).


A Tcl comment line begins with #. More generally, any occurrence of the # sign in a command line means that text to the right of it is considered as comment and is ignored.

Assignment and Interrogation of Variables

The command set is used to assign variables, and a $ sign is used to obtain the value of a variable. All variables in Tcl are stored as strings.

set a 1
set b $a

sets both variables a and b to the string "1".

Global Variables

A variable may be declared global using the global command. In this case all procedures including the declaration can access and modify the variable.

Return Values

The return code of Tcl command is a string, which may used as an argument to another function by surrounding the command with square brackets [ ].

set a [ prog arg1 arg2 ]

Tcl Lists

The concept of a list is used in Tcl to allow a variable to be composed of a number of other variables, in a certain order. The curly braces { } and double quotes (" ") are used to define lists. You will notice a lot of braces in Tcl scripts; sometimes this will reflect explicit construction of a list for the use by the script, more usually they group together items to form complex arguments to Tcl operators like for and if.

set a { 1 2 3 }

The distinction between the use of braces and quotes is significant when the list contains references to Tcl variables. These are substituted by the variable values when quoted, but not when in braces. For example,

set b 2
set t { 1 $b 3 }
puts stdout $t

Will output

1 $b 3


set b 2
set t " 1 $b 3 "
puts stdout $t


1 2 3

Continuation lines

The backslash may be used to continue a Tcl command on another line. For example,

set a \

Print statement - puts

The command puts is used to write a string, and is the simplest way of writing output, either to a file (see Tcl documentation on how to open files) or to stdout.

set a [ myprog arg1 arg2 ]
puts stdout "the answer from myprog was $a"

For loop

for { set i 0 } { $i < 5 } { incr i } {

Block if

if { "$name" == "paul" } then {
} elseif { $code == 0 } then {
} else {

Command list

The Tcl command list will return a list formed from its arguments. It is often useful when constructing complex list structures, especially when the output of a Tcl command is incorporated into the list.

set a [ list b c d [ expr $e + $f ] ]

Declaration and use of a procedure

The command proc is used to declare a procedure. The name of the procedure may then be used as a Tcl command.

proc myproc { arg1 arg2 } {
 .. procedure body...
myproc a b


The Tcl command exec will run an external program, the return value is the output (on stdout) from the program.

set tmp [ exec myprog ]
puts stdout $tmp

This manual was generated using htp, an HTML pre-processor Powered by htp