Introduction to Tcl
last modified November 24, 2026 Tcl 8.6
Tcl, the Tool Command Language, is a small, dynamically typed scripting
language created by John Ousterhout in 1988. It is designed around a simple
idea: a program is a sequence of commands, and every command is a word followed
by arguments. Tcl is used for automation, testing, network tools, embedded
control and, together with the Tk toolkit, desktop applications. This article
introduces the language, the tclsh interpreter and the first
building blocks you need for the rest of the series.
What Tcl is
Tcl has very little syntax. There are no keywords in the usual sense, no declarations and no mandatory types. A script is a list of commands separated by newlines or semicolons, and a command is a command name followed by words. The interpreter substitutes variables and nested commands in each word before it calls the command. That single rule explains most of the language.
Tcl values are strings, although the interpreter keeps internal numeric and list representations for speed. The language ships with a large standard library of commands for strings, lists, dictionaries, files, processes and events. Tk adds widgets on top of the same interpreter, which is why a Tcl script can grow from a command-line tool into a graphical program without changing languages.
The tclsh interpreter
The console interpreter is called tclsh. A Tcl script is an
ordinary text file; by convention it uses the .tcl extension. The
first script prints a line with the puts command and reports the
interpreter version with info patchlevel.
puts "Hello there!" puts "This is Tcl [info patchlevel]."
The first line is a single command: puts receives one argument,
the quoted string. The second line contains square brackets. Tcl runs the
command inside the brackets first, substitutes its result into the string, and
then calls puts with the finished text. This is called command
substitution.
Run the file by passing its name to tclsh. Without a file name,
tclsh starts an interactive prompt where each line is evaluated as
soon as you press Enter. The prompt is useful for trying a command before
adding it to a script.
$ tclsh hello.tcl Hello there! This is Tcl 8.6.12.
Variables and substitution
The set command creates a variable and returns its value. A dollar
sign followed by a name substitutes the variable inside a double-quoted word.
Braces, on the other hand, switch substitution off completely, so the text
inside them is passed literally.
set name "Jane"
set greeting "Hello, $name!"
puts $greeting
puts {Hello, $name!}
puts "The name has [string length $name] characters."
The third line builds a new string while the variable is being set. The fourth
line prints the stored greeting. The fifth line uses braces, so
$name is printed as text instead of being replaced. The last line
combines variable substitution with a nested string length
command.
Notice that puts $greeting does not need quotes. A single word can
be a variable reference, and the value is passed as one argument even when it
contains spaces. Quotes are needed only when the variable is part of a larger
word. This is the same substitution rule from the first script, applied to a
variable instead of a command.
$ tclsh variables.tcl Hello, Jane! Hello, $name! The name has 4 characters.
Commands and command substitution
Every Tcl command returns a value, and square brackets turn that value into a
word. The next script uses three string subcommands to inspect and
change a value. Each result is substituted into a larger string.
set city Bratislava set country Slovakia puts "I live in $city, $country." puts "Upper case: [string toupper $city]" puts "First three letters: [string range $city 0 2]" puts "Position of 'va': [string first va $city]"
The first puts substitutes two variables into one quoted string.
The remaining lines place a string command inside the brackets, so
the outer puts receives the result of the inner command. Command
substitution nests to any depth, which is how Tcl builds short expressions
without a separate expression syntax.
String positions start at zero, so the third character of
Bratislava is the letter a and
string first reports 8 for the two-letter sequence
va. The Tcl string command page lists
all subcommands; the set and
puts pages cover the two commands used here.
$ tclsh strings.tcl I live in Bratislava, Slovakia. Upper case: BRATISLAVA First three letters: Bra Position of 'va': 8
Finding out about the interpreter
Tcl exposes information about itself through the info command and
the tcl_platform array. The next script prints the patch level,
the language version and two platform fields. This is often the first thing to
check when a script behaves differently on another machine.
puts "Tcl patch level: [info patchlevel]" puts "Tcl version: [info tclversion]" puts "Platform: $tcl_platform(platform)" puts "Machine: $tcl_platform(machine)"
info patchlevel returns the exact release, while
info tclversion returns the major and minor version. The
tcl_platform array is a normal Tcl array; its element names are
written in parentheses after the array name. The two values printed here tell
you that the script is running on a 64-bit Linux system.
The series targets Tcl 8.6, the version installed on most current Linux systems. Tcl 9 exists but is not installed here, so the articles note the few places where 8.6 behaviour matters. The Tcl info command page documents the other fields available at run time.
$ tclsh version.tcl Tcl patch level: 8.6.12 Tcl version: 8.6 Platform: unix Machine: x86_64
Tcl and Tk
The graphical interpreter is called wish. It is the same Tcl
interpreter with the Tk toolkit loaded. A Tk script creates widgets, arranges
them with a geometry manager, and then enters the event loop with
mainloop. The following preview creates a small window and closes
it after one second so the example can run unattended.
package require Tk
wm title . "Tcl and Tk"
wm geometry . 320x120
label .greeting -text "Hello there!" -font {Helvetica 18}
pack .greeting -padx 20 -pady 30
after 1000 {puts "Window closed by timer"; destroy .}
package require Tk loads the toolkit. The dot is the main window,
and wm title and wm geometry set its title and size.
The label command creates a widget named .greeting,
and pack places it in the window with padding. The last line asks
Tk to run a short script after 1000 milliseconds; that script prints a message
and destroys the window. In a normal program the window stays open inside
mainloop, which the Tk articles cover in detail.
Tk is optional. A script that never calls package require Tk runs
in tclsh and needs no display. Keeping the console and graphical
parts separate is a common way to test the logic of an application without
opening a window.
$ wish hello_gui.tcl Window closed by timer
How to read the rest of the series
The next article explains the lexical structure of Tcl: how commands and words are separated, when substitution happens and how comments work. After that the series moves through values, expressions, flow control, strings, lists, dictionaries, procedures, namespaces and error handling. Each article builds on the previous one and ends with a complete, runnable script rather than an isolated fragment.
If you already know another scripting language, resist the temptation to read Tcl as a syntax error away from Python or Ruby. Tcl really has one core rule, substitution, and everything else follows from it. The small scripts in this article are the foundation for that rule.
Source
The official documentation is the best reference for the commands used here. The Tcl 8.6 manual covers the language and the standard library, and the Tk 8.6 manual covers the widgets.
This article introduced Tcl, the tclsh and wish
interpreters, variables, command substitution and the first steps with Tk.
Continue with the refreshed Tcl lexical
structure article to learn exactly how the interpreter splits a script into
commands and words, or go straight to Tcl values and
types to see how it stores and classifies values.
Author
List all Tcl Tutorials.