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:
| |
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!"- Theputscommand 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:
- Everything is a command -
putsis a command, “Hello, World!” is its argument - Minimal syntax - No semicolons, no parentheses around arguments
- Strings are natural - Double quotes create a string with substitution enabled
- 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:
| |
Understanding the Docker Command
docker run --rm- Run a container and remove it when done-v $(pwd):/app- Mount the current directory as/appinside the container-w /app- Set the working directory to/appefrecon/tcl:latest- The Tcl Docker image (entrypoint is tclsh)/app/hello.tcl- The script file to execute
Running Locally
If you have Tcl installed:
| |
Installing Tcl
macOS (Homebrew):
| |
Ubuntu/Debian:
| |
Windows: Download from ActiveTcl or use the built-in tclsh with Git Bash.
Fedora/RHEL:
| |
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
| |
Yes, a one-line program! Tcl doesn’t require any structure for simple scripts.
Using Braces Instead of Quotes
| |
Braces {} prevent substitution. For a literal string with no variables, braces work identically to quotes.
Using String Concatenation
| |
Variables are substituted when inside double quotes. The $ prefix retrieves the variable’s value.
Using the append Command
| |
The append command modifies a variable in place.
Multi-line Output
| |
Each puts produces a new line.
Using format
| |
The format command works like printf in C.
Key Concepts
1. The puts Command
The puts command is Tcl’s primary output mechanism:
| |
2. Comments
Tcl uses # for comments:
| |
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:
| |
4. Command Structure
Every Tcl command follows the same pattern:
| |
There are no exceptions—even control structures like if and while are commands.
The Minimal Program
Technically, the smallest valid Tcl “Hello World” is:
| |
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
| |
Confusing Quotes and Braces
| |
Missing Semicolon for Inline Comments
| |
Spaces in Command Substitution
| |
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