Tcl Tutorial Lesson 0a

Running Tcl

When you have installed Tcl, the program you will then call to utilize it is tclsh. For instance, if you write some code to a file hello.tcl, and you want to execute it, you would do it like so: tclsh hello.tcl. Depending on the version of Tcl installed, and the operating system distribution you use, the tclsh program may be a link to the real executable, which may be named tclsh8.6, or tclsh86.exe on Microsoft Windows.

The tclsh program runs Tcl programs, but it also allows interactive use: You can either start it with a script file on the command line, in which case it runs the script to completion and then exits, or you can start it without any arguments, in which case you get an interactive prompt, usually a % symbol where you type in commands. Tcl will then execute and display the result, or any error messages that result. To exit the interpreter, type exit and press Return. For example:

Tcl Tutorial Picture 0a

Playing around with the interactive interpreter is a great way to learn how to use Tcl. Most Tcl commands will produce a helpful error message explaining how they are used if you just run them without arguments. You can get a list of all the commands that your interpreter knows about by running info commands.

Setting up tclsh for comfortable interactive use on Unix-like systems

tclsh itself does not offer command history or line editing, which can make interactive use a bit cumbersome compared to most modern command-line shells. However, on systems that have GNU Readline available, that functionality can be added by using tclsh in conjunction with rlwrap, a Readline wrapper. To do that, simply run:

  rlwrap tclsh

To make this a permanent setting, you can make tclsh an alias of rlwrap tclsh in your shell's run control file (e.g., ~/.bashrc when using the Bash shell).

Tcl interpreters other than tclsh

The tclsh executable is just one way of starting a Tcl interpreter. Another common executable, which may be installed on your system, is the wish, or WIndowing SHell. This is a version of Tcl that automatically loads the Tk extension for building graphical user interfaces (GUIs). This tutorial does not cover Tk, and so we will not use the wish interpreter here. Other options are also available, providing more functional environments for developing and debugging code than that provided by the standard tclsh. One very popular choice is the TkCon enhanced interpreter, written by Jeff Hobbs. The Eclipse IDE offers good Tcl support, in the form of the DLTK extension, and the Tcl'ers Wiki offers a list of IDEs with Tcl support and a comprehensive catalogue of Tcl source code editors . Don't panic, though! If you don't know how to use a sophisticated development environment, it is still very easy to write Tcl code by hand in a simple text editor (such as Notepad).

Some conventions

In the following lessons, we will use the convention that interactive sessions are indicated by the % prompt: The commands are preceded by the percent sign and the result is shown directly below, as it would appear if you ran them in tclsh.

As you will see in the code fragments, Tcl does not make a syntactical difference between values and variable names. To get the value of a variable you ordinarily use the "$" sign in front of the name of the variable. But sometimes the name of the variable is required. To make this clear, we use a name like "varName". But you should be aware that this merely a convention in this tutorial. If used in a place where a value is expected, the value would actually be "varName" and not the value of the variable "varName".

In the online tutorial, the results of many code fragments are shown only after pressing the button "Show". You are supposed to try and imagine yourself what the output would be.