Hello World (first programs) · Beginner · 15 min
Hello World in Tcl: tclsh, everything is a string, and Tk
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Tcl 9.0.3 (tcl9.0 package) · upstream Tcl 9.1.0
You will install Tcl 9.0, print Hello World from tclsh and from a script, read a name from the keyboard, and learn the three substitution rules that make up nearly all of Tcl's syntax.
On this page
Tcl (say "tickle", short for Tool Command Language) was written by John Ousterhout at Berkeley in 1988 as a small language that any C program could embed as its command language. It is still doing that job. When an engineer types commands into a chip-design tool, a network test rig or a router's shell, there is a good chance the language underneath is Tcl.
Tcl's rules are unusually few. Every line is a command: the first word is the command's name and the rest are its arguments. Every value is a string. if, while and even proc (which defines functions) are ordinary commands, not keywords. Three kinds of substitution, $ for variables, [ ] for running a command and `` for special characters, cover almost everything else. Once you see those rules work, you can read most Tcl code.
You use Tcl 9.0, which Ubuntu 26.04 packages as 9.0.3. Upstream has released 9.0.4 and 9.1.0; nothing on this page differs between them. You work in a throwaway folder, ~/hello-tcl-demo, and delete it at the end.
Step 1 · Install Tcl 9.0
On Ubuntu 26.04, install the tcl9.0 package and ask the interpreter for its exact version:
sudo apt update
sudo apt install -y tcl9.0
echo 'puts [info patchlevel]' | tclsh9.0Install tcl9.0 by name. The plain tcl package on Ubuntu 26.04 still installs Tcl 8.6, and its command, tclsh, starts 8.6. The versioned name tclsh9.0 lets both live side by side, and this page always types it so you know which one runs.
The last line sends a one-line Tcl script to tclsh9.0 through a pipe. info patchlevel is a command that returns the full version number, the square brackets run it, and puts prints the result. You meet each of those pieces properly below.
On Windows, either use WSL (install Ubuntu from the Microsoft Store and follow the commands here) or download a Windows build: Magicsplat's free Tcl/Tk installer and ActiveState's ActiveTcl are the two common ones. On macOS, Homebrew's tcl-tk formula installs a recent Tcl; depending on the release the command may be tclsh9.0 or tclsh. Whatever you install, check the version with info patchlevel before you continue.
Check it worked
The last command prints 9.0.3, or a later 9.0 release. If it prints 8.6 followed by a number, you are running the older tclsh; type tclsh9.0. If you see command not found, read the end of the apt install output for an error and run it again.
Step 2 · Say hello in tclsh
Start the interactive shell:
tclsh9.0You get a % prompt. Type these lines one at a time, pressing Enter after each:
puts "Hello, World!"
expr {2 + 3}
set name Ada
exitputs "Hello, World!"runs the commandputswith one argument. The double quotes groupHello,andWorld!into a single word, soputsreceives one string and prints it with a newline.expr {2 + 3}runs theexprcommand, which does arithmetic. At the prompt, tclsh shows each command's result, so you see5. Tcl has no arithmetic outsideexpr;2 + 3on its own would try to run a command called2.set name AdastoresAdain the variablename.setreturns the value it stored, so the prompt echoesAda.exitleaves the shell. Ctrl+D also works.
You can also pipe a script into tclsh9.0 without opening the prompt:
echo 'puts [expr {2 + 3}]' | tclsh9.0Check it worked
At the prompt, puts prints Hello, World!, expr {2 + 3} shows 5 and set name Ada shows Ada. The piped command prints 5 and returns you to your shell.
Step 3 · Write and run hello.tcl
Make the demo folder and create the script:
mkdir -p ~/hello-tcl-demo
cd ~/hello-tcl-demo
cat > hello.tcl <<'TCL'
#!/usr/bin/env tclsh9.0
# hello.tcl: the first Tcl script
puts "Hello, World!"
TCL
tclsh9.0 hello.tclYou can type the three lines into your editor and save them as hello.tcl instead. Line by line:
#!/usr/bin/env tclsh9.0is the shebang. Linux and macOS read it to decide which program runs the file when you start it directly. To Tcl the line is a comment, so it does no harm when you runtclsh9.0 hello.tcl.# hello.tcl: ...is a comment. In Tcl,#starts a comment only where a command could start. This matters later.puts "Hello, World!"is the whole program. There is nomainfunction, nothing to import and no semicolon; tclsh reads the file and runs each command from top to bottom.
Make the file executable and run it without naming the interpreter:
cd ~/hello-tcl-demo
chmod +x hello.tcl
./hello.tclCheck it worked
Both tclsh9.0 hello.tcl and ./hello.tcl print Hello, World!. If ./hello.tcl says Permission denied, the chmod +x line did not run.
Step 4 · Read a name from the keyboard
Create ask.tcl, which asks for your name and greets you:
cd ~/hello-tcl-demo
cat > ask.tcl <<'TCL'
puts -nonewline "What is your name? "
flush stdout
if {[gets stdin name] < 0 || $name eq ""} {
set name "stranger"
}
puts "Hello, $name!"
TCLWhat each line does:
puts -nonewline "..."prints without a newline, so your answer appears on the same line as the question.flush stdoutpushes that text out now. Tcl buffers output, and text without a newline can sit in the buffer while the program waits for you to type. Forgettingflushis a classic reason a prompt "doesn't appear".gets stdin namereads one line from standard input into the variablename, without the newline. It returns the number of characters read, or-1at the end of input (an empty pipe, or Ctrl+D).if {...} {...}is a command with two arguments: a condition and a body. Both are in braces.||means "or", andeqcompares two strings.$nameinside double quotes is replaced by the variable's value, so"Hello, $name!"becomesHello, Ada!.
Run it and type your name:
tclsh9.0 ask.tclThen feed it a name from a pipe, and once with no input at all:
cd ~/hello-tcl-demo
echo "Ada" | tclsh9.0 ask.tcl
tclsh9.0 ask.tcl < /dev/nullCheck it worked
The pipe prints What is your name? Hello, Ada! on one line. The run with no input prints What is your name? Hello, stranger!.
Step 5 · Use the three substitutions
This is the core of Tcl. Before a command runs, Tcl scans its words once and makes these replacements:
$nameis replaced by the value of the variablename.[command args]runs that command and is replaced by its result.` gives the next character its literal meaning, or a special one such as `\nfor a newline.
How a word is grouped decides whether substitution happens. Double quotes group words and still substitute. Braces group words and substitute nothing. Create subst.tcl to see each case:
cd ~/hello-tcl-demo
cat > subst.tcl <<'TCL'
set name "Ada"
set year 2026
puts "Hello, $name"
puts {Hello, $name}
puts "Your name has [string length $name] letters"
puts "Next year is [expr {$year + 1}]"
puts "That costs \$5"
set greeting [string toupper "hello, $name"]
puts $greeting
TCL
tclsh9.0 subst.tclLine by line:
set name "Ada"andset year 2026create two variables. Note there is no$when you set a variable, only when you read it.puts "Hello, $name"substitutes inside quotes and printsHello, Ada.puts {Hello, $name}uses braces, so nothing is replaced and you seeHello, $nameexactly as written.[string length $name]runs thestring lengthcommand onAdaand drops its result,3, into the sentence.[expr {$year + 1}]does the arithmetic. Always put theexprexpression in braces:exprthen reads the variables itself, which is faster and avoids surprises with values that contain spaces or brackets.\$5stops Tcl treating$5as a variable.set greeting [string toupper ...]stores the result of a command in a variable, the Tcl way of writinggreeting = ....
Check it worked
You see six lines: Hello, Ada, then Hello, $name, Your name has 3 letters, Next year is 2027, That costs $5 and HELLO, ADA. If the second line printed Hello, Ada, you typed double quotes where the braces should be.
The braces after if in the previous step work the same way. if receives its condition and body as unsubstituted strings and evaluates them itself, which is why a Tcl block uses braces: a body is just a string that a command chooses to run.
Step 6 · See that every value is a string
Tcl has no separate types you declare. A number, a list and a block of code are all strings, and each command reads the string the way it needs to. Create strings.tcl:
cd ~/hello-tcl-demo
cat > strings.tcl <<'TCL'
set n 42
puts [expr {$n + 1}] ;# used as a number: 43
puts [string length $n] ;# used as text: 2 characters
set cities {Lagos Nairobi Accra}
puts [llength $cities] ;# used as a list: 3 items
puts [lindex $cities 0] ;# first item: Lagos
puts [string length $cities] ;# used as text: 19 characters
set word "Ọlá"
puts [string length $word]
puts [string length [encoding convertto utf-8 $word]]
puts [expr {010 + 1}]
TCL
tclsh9.0 strings.tclLine by line:
set n 42stores the string42.exprreads it as a number and adds 1;string lengthreads the same value as text and counts two characters.;#starts a comment after a command. The;ends the command first, so the#is at the start of a new one. A bare#afterputswould be passed toputsas an extra argument.{Lagos Nairobi Accra}is a list because list commands such asllengthandlindexsplit it on spaces. Tostring lengthit is 19 characters of text. Lists start at index 0.string length $wordcounts characters. The Yoruba name Ọlá has three.encoding convertto utf-8 $wordturns the text into its UTF-8 bytes, and the length of that is six, because Ọ and á each take more than one byte. If you check a length limit on a form or an SMS for names in Yoruba, Igbo, Hausa, Amharic or Arabic, count characters, not bytes.expr {010 + 1}shows a Tcl 9 change. A number with a leading zero is now plain decimal, so this is 11. In Tcl 8.6 it was read as octal (base 8) and gave 9. Write0o10when you really mean octal.
Two more Tcl 9 changes you meet early: script files are read as UTF-8 on every system, which is why the Ọlá line works without any setting, and file commands no longer expand ~ to your home folder. Write [file home] instead, for example file join [file home] notes.txt.
Check it worked
You see eight lines, one value each, in this order: 43, 2, 3, Lagos, 19, 3, 6 and 11. If the last line is 9, you are running Tcl 8.6.
Step 7 · Define a procedure and read command-line arguments
proc creates a new command. It is itself an ordinary command with three arguments: a name, a list of parameters and a body. Create greet.tcl:
cd ~/hello-tcl-demo
cat > greet.tcl <<'TCL'
proc greet {who {greeting Hello}} {
return "$greeting, $who!"
}
if {$argc == 0} {
set names {World}
} else {
set names $argv
}
foreach who $names {
puts [greet $who]
}
puts [greet Zainab Salaam]
TCL
tclsh9.0 greet.tcl
tclsh9.0 greet.tcl Ada KwameLine by line:
proc greet {who {greeting Hello}} {...}defines a command calledgreet.whois a required parameter.{greeting Hello}is a parameter with a default value, used when the caller leaves it out.return "$greeting, $who!"sends the finished string back to the caller.$argcis the number of words after the script name on the command line, and$argvis those words as a list. tclsh sets both before your script runs.foreach who $names {...}runs the body once per list item, withwhoset to that item.[greet $who]calls your new command exactly like a built-in one.greet Zainab Salaampasses both arguments, so the default is replaced.
Check it worked
The first run prints Hello, World! then Salaam, Zainab!. The second prints Hello, Ada!, Hello, Kwame! and Salaam, Zainab!.
Step 8 · Open a window with Tk
Tk is Tcl's graphical toolkit, and the reason many people first met Tcl. It is also what Python's built-in tkinter module drives behind the scenes. This step needs a desktop session, so skip the running part on a server. Create the script:
cd ~/hello-tcl-demo
cat > hello-tk.tcl <<'TCL'
package require Tk
label .greeting -text "Hello, World!" -padx 40 -pady 20
pack .greeting
TCLpackage require Tkloads the Tk library into Tcl.label .greeting -text ...creates a label widget named.greeting. Widget names are paths:.is the main window, so.greetinglives inside it.-padxand-padyadd space around the text, in pixels.pack .greetingplaces the label in its window. Until a widget is packed (or placed withgrid), it exists but is not shown.
On an Ubuntu desktop, install Tk and run the script with wish9.0, the windowing shell:
sudo apt install -y tk9.0
wish9.0 hello-tk.tclCheck it worked
The cat command creates hello-tk.tcl with no output. On a desktop, wish9.0 opens a small window showing Hello, World!, and the program keeps running until you close the window. That is the difference from tclsh: wish waits for events, such as clicks and key presses, after the script ends.
Step 9 · Avoid common mistakes and know where Tcl is used
Most early Tcl errors come from the word rules. Try the most common one, a missing space before a brace:
cd ~/hello-tcl-demo
cat > bad.tcl <<'TCL'
set x 5
if {$x > 3}{
puts "big"
}
TCL
tclsh9.0 bad.tcl || echo "exit code: $?"Tcl splits words on spaces, so {$x > 3}{ is one malformed word, not a condition followed by a body. The other mistakes beginners hit:
- A comment after a command without
;.puts "hi" # say hipasses#,sayandhitoputsand fails with something likewrong # args. Writeputs "hi" ;# say hi. $in the wrong place.set $name Adasets a variable named after the value ofname. Useset name Adato set and$nameto read.- Arithmetic outside
expr.set total $a + $bis three words and an error. Writeset total [expr {$a + $b}]. - The wrong interpreter. Plain
tclshmay be 8.6. If leading-zero numbers or~paths behave differently from this page, checkinfo patchlevel. - Braces inside comments. Tcl matches braces before it knows a line is a comment, so an unbalanced
{in a comment inside aprocbody can break the whole procedure.
Where you will meet Tcl at work:
- Chip design. Electronic design automation tools for FPGAs and chips, such as AMD Vivado and Altera Quartus, use Tcl as their command and scripting language. Build flows are Tcl scripts.
- Test automation. Expect, built on Tcl, drives interactive programs: it waits for a prompt, sends a reply, and checks the output. Network and QA teams use it to script router and switch command lines, and the SQLite project's own test suite is written in Tcl.
- Embedded configuration. Programs embed Tcl so users can script them; network devices such as some Cisco routers offer a Tcl shell, and F5 load balancers' iRules are written in a Tcl dialect.
When you have finished, delete the demo folder:
cd ~
rm -rf ~/hello-tcl-demoKeep tcl9.0 if you plan to carry on. To remove it, run sudo apt remove tcl9.0 (and tk9.0 if you installed it).
Check it worked
bad.tcl fails with an error containing extra characters after close-brace, then prints exit code: 1. Add a space so the line reads if {$x > 3} { and it prints big. After the cleanup, ls ~/hello-tcl-demo reports No such file or directory.
Next steps
- Compare Tcl with the other small embeddable language, built around tables instead of strings: Hello World in Lua.
- See how the shell handles words, quoting and substitution, which Tcl's rules resemble: Hello World in Bash.
- Try another language with deep roots in system administration and text processing: Hello World in Perl.
Related tutorials
- Hello World in Lua: tables, 1-based arrays and embeddingYou will install Lua 5.5, run print in the interpreter and from a script, read a name from the keyboard, and use a single table as both a list and a map without falling into the 1-based indexing trap.
- Hello World in Bash: a script with arguments and exit codesYou will write and run a Bash 5.3 script that greets a name from the command line or the keyboard, rejects bad input with its own exit code, runs in strict mode and passes ShellCheck.
- Hello World in Perl: strict, warnings and a one-linerYou will install Perl 5.44, run a Hello World script that has strict and warnings on, read a name from the keyboard, and summarise a log file with a one-line Perl command.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials