Tcl - Basic Syntax


Tcl is quite simple to learn and let's start creating our first Tcl program!

First Tcl Program

Let us write a simple Tcl program. All Tcl files will have an extension, i.e., .tcl. So, put the following source code in a test.tcl file.

Live Demo

puts "Hello, World!" 

Assuming, Tcl environment is setup correctly; let's run the program after switching to file's directory and then execute the program using −

$ tclsh test.tcl

We will get the following output −

Hello, World!

Let us now see the basic structure of Tcl program, so that it will be easy for you to understand basic building blocks of the Tcl language. In Tcl, we use new line or semicolon to terminate the previous line of code. But semicolon is not necessary, if you are using newline for each command.


Comments are like helping text in your Tcl program and the interpreter ignores them. Comments can be written using a hash_(#) sign in the beginning.

Live Demo

# my first program in Tcl
puts "Hello World!" 

When the above code is executed, it produces the following result −

Hello World!

Multiline or block comment is written using 'if' with condition '0'. An example is shown below.

Live Demo

if 0 {
   my first program in Tcl program
   Its very simple
puts "Hello World!" 

When the above code is executed, it produces the following result −

Hello World!

Inline comments use ;#. An example is given below.

Live Demo

puts "Hello World!" ;# my first print in Tcl program

When the above code is executed, it produces the following result −

Hello World!


A Tcl identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, dollars ($) , and digits (0 to 9).

Tcl does not allow punctuation characters such as @, and % within identifiers. Tcl is a case sensitive_ language. Thus Manpower and manpower are two different identifiers in Tcl. Here are some of the examples of acceptable identifiers −

mohd       zara    abc   move_name  a_123
myname50   _temp   j     a23b9      retVal

Reserved Words

The following list shows a few of the reserved words in Tcl. These reserved words may not be used as constant or variable or any other identifier names.

after append array auto_execok
auto_import auto_load auto_load_index auto_qualify
binary Bgerror break catch
cd Clock close concat
continue Dde default else
elseif Encoding eof error
eval Exec exit expr
fblocked Fconfigure fcopy file
fileevent Flush for foreach
format Gets glob global
history If info interp
join Lappend lindex linsert
list Llength load lrange
lreplace Lsearch lsort namespace
open Package pid pkg_mkIndex
proc Puts pwd read
regexp Regsub rename resource
return Scan seek set
socket Source split string
subst Switch tclLog tell
time Trace unknown unset
update Uplevel upvar variable
vwait While

Whitespace in Tcl

A line containing only whitespace, possibly with a comment, is known as a blank line, and a Tcl interpreter totally ignores it.

Whitespace is the term used in Tcl to describe blanks, tabs, newline characters, and comments. Whitespace separates one part of a statement from another and enables the interpreter to identify where one element in a statement, such as puts, ends and the next element begins. Therefore, in the following statement −


puts "Hello World!" 

There must be at least one whitespace character (usually a space) between “puts” and "Hello World!" for the interpreter to be able to distinguish them. On the other hand, in the following statement −

Live Demo

puts [expr 3 + 2] ;# print sum of the 3 and 2

When the above code is executed, it produces the following result −


No whitespace characters are necessary between 3 and +, or between + and 2; although, you are free to include some if you wish for the readability purpose.