Hello World with Tcl

Advertisement

Advertisement

Tcl is a language similar Bash because every line is a command and it can be scripted similar to shell scripts. Tcl can be used by itself but it typically embedded inside larger applications. Tcl with Tk toolkit allows you to create GUI applications. Once nice thing about Tcl/Tk is that it is widely available and works on multiple platforms. It does not create the nicest looking GUIs because it does not use native windows, but they are effective. Let's look at the most basic Hello World program. Let's name it hello.tcl.

puts "Hello, world!"

Now you can run it with

tclsh hello.tcl

Well, that was easy wasn't it? You just put a string on the screen with puts and that's it. We could explicitly exit with a success code by adding exit 0 to the end like this:

puts "Hello, world!"
exit 0

Exiting with a 1 would indicate there was an error. By returning that information, other programs can make a decision based on the success or failure. Tcl can also be used interactively like Python or Bash too. Just run tclsh and start typing in commands like puts. Tcl programs can also be run as scripts with a shebang at the top if you are in a Unix system. You can then run the script by typing:

./hello.tcl

To do that, add a shebang at the top with the path to your Tcl shell. You can find that out by typing which tclsh

#!/usr/bin/tclsh
puts "Hello!"

Advertisement

Advertisement