Beginner

Hello World in Tcl

Your first Tcl program - the classic Hello World example with Docker setup using the tclsh interpreter

Every programming journey starts with Hello World. Tcl makes this remarkably simple—reflecting its design philosophy that scripting should be easy and intuitive.

The Code

Create a file named hello.tcl:

1
2
# Hello World in Tcl
puts "Hello, World!"

That’s it! Just two lines—a comment and a single puts command.

Understanding the Code

Let’s break down this simple program:

  • # Hello World in Tcl - A comment. In Tcl, # starts a comment that extends to the end of the line.
  • puts "Hello, World!" - The puts command outputs text to the console and adds a newline. The string is enclosed in double quotes.

The Tcl Philosophy in Action

This simple example demonstrates Tcl’s core design principles:

  1. Everything is a command - puts is a command, “Hello, World!” is its argument
  2. Minimal syntax - No semicolons, no parentheses around arguments
  3. Strings are natural - Double quotes create a string with substitution enabled
  4. No boilerplate - No main function, no imports, no declarations

Running with Docker

The easiest way to run Tcl without installing anything locally is using Docker:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Pull the Tcl image
docker pull efrecon/tcl:latest

# Create the source file
cat > hello.tcl << 'EOF'
# Hello World in Tcl
puts "Hello, World!"
EOF

# Run the program
docker run --rm -v $(pwd):/app -w /app efrecon/tcl:latest /app/hello.tcl

Understanding the Docker Command

  • docker run --rm - Run a container and remove it when done
  • -v $(pwd):/app - Mount the current directory as /app inside the container
  • -w /app - Set the working directory to /app
  • efrecon/tcl:latest - The Tcl Docker image (entrypoint is tclsh)
  • /app/hello.tcl - The script file to execute

Running Locally

If you have Tcl installed:

1
2
# Simply run the program
tclsh hello.tcl

Installing Tcl

macOS (Homebrew):

1
brew install tcl-tk

Ubuntu/Debian:

1
sudo apt install tcl

Windows: Download from ActiveTcl or use the built-in tclsh with Git Bash.

Fedora/RHEL:

1
sudo dnf install tcl

Expected Output

Hello, World!

Clean, simple output—exactly what you asked for!

Alternative Styles

Tcl’s flexibility allows several ways to write Hello World:

Without the Comment

1
puts "Hello, World!"

Yes, a one-line program! Tcl doesn’t require any structure for simple scripts.

Using Braces Instead of Quotes

1
puts {Hello, World!}

Braces {} prevent substitution. For a literal string with no variables, braces work identically to quotes.

Using String Concatenation

1
2
3
set greeting "Hello"
set target "World"
puts "$greeting, $target!"

Variables are substituted when inside double quotes. The $ prefix retrieves the variable’s value.

Using the append Command

1
2
3
set message "Hello"
append message ", World!"
puts $message

The append command modifies a variable in place.

Multi-line Output

1
2
3
# Multiple puts statements
puts "Hello,"
puts "World!"

Each puts produces a new line.

Using format

1
puts [format "%s, %s!" "Hello" "World"]

The format command works like printf in C.

Key Concepts

1. The puts Command

The puts command is Tcl’s primary output mechanism:

1
2
3
4
5
puts "Text in quotes"           ;# String with substitution
puts {Text in braces}           ;# Literal string
puts $variable                  ;# Variable value
puts "Value: $variable"         ;# String with embedded variable
puts [expr {2 + 2}]             ;# Command substitution result: 4

2. Comments

Tcl uses # for comments:

1
2
3
# This is a full-line comment

puts "Hello"  ;# This is an inline comment (note the semicolon!)

Important: For inline comments, you need a semicolon before the # because Tcl sees everything as command arguments.

3. Quoting Rules

Tcl has two quoting mechanisms:

1
2
3
4
5
6
# Double quotes - substitution happens
set name "Tcl"
puts "Hello, $name!"    ;# Output: Hello, Tcl!

# Braces - no substitution
puts {Hello, $name!}    ;# Output: Hello, $name!

4. Command Structure

Every Tcl command follows the same pattern:

1
commandName arg1 arg2 arg3 ...

There are no exceptions—even control structures like if and while are commands.

The Minimal Program

Technically, the smallest valid Tcl “Hello World” is:

1
puts "Hello, World!"

No spaces required around the string! However, for readability, always use proper spacing.

A Bit of History

When John Ousterhout created Tcl in 1988 at UC Berkeley, he had a specific vision: applications needed a command language, but every tool was inventing its own incompatible scripting language. Tcl would be the reusable command language that any application could embed.

The puts command exemplifies this—it’s named after the C standard library function, making it familiar to C programmers who might embed Tcl in their applications. The language was designed to be a bridge between the application and its users.

This same program would have run on Unix workstations in the late 1980s, powered Tk GUI applications in the 1990s, scripted Cisco routers in the 2000s, and now runs in Docker containers in the 2020s. Tcl’s simplicity has given it remarkable longevity.

Why Learn Tcl Today?

While Tcl isn’t the trendiest language, it offers:

  • Network automation - Cisco IOS and many network devices embed Tcl
  • EDA scripting - Chip design tools universally support Tcl
  • Expect scripting - Automate SSH, Telnet, and interactive programs
  • GUI prototyping - Tk remains one of the simplest ways to create GUIs
  • Embedded scripting - Many applications still embed Tcl for extensibility

Common Beginner Mistakes

Forgetting Quote Marks

1
2
puts Hello, World!     ;# Error! Tcl sees multiple arguments
puts "Hello, World!"   ;# Correct - single string argument

Confusing Quotes and Braces

1
2
3
set x 5
puts "x is $x"    ;# Output: x is 5 (substitution happens)
puts {x is $x}    ;# Output: x is $x (no substitution)

Missing Semicolon for Inline Comments

1
2
puts "Hello" # This is NOT a comment - it's an error!
puts "Hello" ;# This IS a comment (semicolon required)

Spaces in Command Substitution

1
2
set result [ expr {2+2} ]   ;# Works, but inconsistent
set result [expr {2+2}]     ;# Better - no spaces inside brackets

Next Steps

Continue to Variables and Data Types to learn about Tcl’s unique approach to data, where everything is a string.

Running Today

All examples can be run using Docker:

docker pull efrecon/tcl:latest
Last updated: