Skip to main content

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:

Bash
sudo apt update
sudo apt install -y tcl9.0
echo 'puts [info patchlevel]' | tclsh9.0

Install 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:

Bash
tclsh9.0

You get a % prompt. Type these lines one at a time, pressing Enter after each:

Text
puts "Hello, World!"
expr {2 + 3}
set name Ada
exit
  • puts "Hello, World!" runs the command puts with one argument. The double quotes group Hello, and World! into a single word, so puts receives one string and prints it with a newline.
  • expr {2 + 3} runs the expr command, which does arithmetic. At the prompt, tclsh shows each command's result, so you see 5. Tcl has no arithmetic outside expr; 2 + 3 on its own would try to run a command called 2.
  • set name Ada stores Ada in the variable name. set returns the value it stored, so the prompt echoes Ada.
  • exit leaves the shell. Ctrl+D also works.

You can also pipe a script into tclsh9.0 without opening the prompt:

Bash
echo 'puts [expr {2 + 3}]' | tclsh9.0

Check 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:

Bash
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.tcl

You can type the three lines into your editor and save them as hello.tcl instead. Line by line:

  • #!/usr/bin/env tclsh9.0 is 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 run tclsh9.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 no main function, 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:

Bash
cd ~/hello-tcl-demo
chmod +x hello.tcl
./hello.tcl

Check 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:

Bash
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!"
TCL

What each line does:

  • puts -nonewline "..." prints without a newline, so your answer appears on the same line as the question.
  • flush stdout pushes 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. Forgetting flush is a classic reason a prompt "doesn't appear".
  • gets stdin name reads one line from standard input into the variable name, without the newline. It returns the number of characters read, or -1 at 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", and eq compares two strings.
  • $name inside double quotes is replaced by the variable's value, so "Hello, $name!" becomes Hello, Ada!.

Run it and type your name:

Bash
tclsh9.0 ask.tcl

Then feed it a name from a pipe, and once with no input at all:

Bash
cd ~/hello-tcl-demo
echo "Ada" | tclsh9.0 ask.tcl
tclsh9.0 ask.tcl < /dev/null

Check 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:

  • $name is replaced by the value of the variable name.
  • [command args] runs that command and is replaced by its result.
  • ` gives the next character its literal meaning, or a special one such as `\n for 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:

Bash
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.tcl

Line by line:

  • set name "Ada" and set year 2026 create two variables. Note there is no $ when you set a variable, only when you read it.
  • puts "Hello, $name" substitutes inside quotes and prints Hello, Ada.
  • puts {Hello, $name} uses braces, so nothing is replaced and you see Hello, $name exactly as written.
  • [string length $name] runs the string length command on Ada and drops its result, 3, into the sentence.
  • [expr {$year + 1}] does the arithmetic. Always put the expr expression in braces: expr then reads the variables itself, which is faster and avoids surprises with values that contain spaces or brackets.
  • \$5 stops Tcl treating $5 as a variable.
  • set greeting [string toupper ...] stores the result of a command in a variable, the Tcl way of writing greeting = ....

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:

Bash
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.tcl

Line by line:

  • set n 42 stores the string 42. expr reads it as a number and adds 1; string length reads 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 # after puts would be passed to puts as an extra argument.
  • {Lagos Nairobi Accra} is a list because list commands such as llength and lindex split it on spaces. To string length it is 19 characters of text. Lists start at index 0.
  • string length $word counts characters. The Yoruba name Ọlá has three.
  • encoding convertto utf-8 $word turns 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. Write 0o10 when 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:

Bash
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 Kwame

Line by line:

  • proc greet {who {greeting Hello}} {...} defines a command called greet. who is 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.
  • $argc is the number of words after the script name on the command line, and $argv is those words as a list. tclsh sets both before your script runs.
  • foreach who $names {...} runs the body once per list item, with who set to that item.
  • [greet $who] calls your new command exactly like a built-in one.
  • greet Zainab Salaam passes 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:

Bash
cd ~/hello-tcl-demo
cat > hello-tk.tcl <<'TCL'
package require Tk
label .greeting -text "Hello, World!" -padx 40 -pady 20
pack .greeting
TCL
  • package require Tk loads the Tk library into Tcl.
  • label .greeting -text ... creates a label widget named .greeting. Widget names are paths: . is the main window, so .greeting lives inside it. -padx and -pady add space around the text, in pixels.
  • pack .greeting places the label in its window. Until a widget is packed (or placed with grid), it exists but is not shown.

On an Ubuntu desktop, install Tk and run the script with wish9.0, the windowing shell:

Bash
sudo apt install -y tk9.0
wish9.0 hello-tk.tcl

Check 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:

Bash
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 hi passes #, say and hi to puts and fails with something like wrong # args. Write puts "hi" ;# say hi.
  • $ in the wrong place. set $name Ada sets a variable named after the value of name. Use set name Ada to set and $name to read.
  • Arithmetic outside expr. set total $a + $b is three words and an error. Write set total [expr {$a + $b}].
  • The wrong interpreter. Plain tclsh may be 8.6. If leading-zero numbers or ~ paths behave differently from this page, check info patchlevel.
  • Braces inside comments. Tcl matches braces before it knows a line is a comment, so an unbalanced { in a comment inside a proc body 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:

Bash
cd ~
rm -rf ~/hello-tcl-demo

Keep 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.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.