Run Tcl Scripts on Linux with tclsh

Tcl script flowing through an interpreter to terminal output

Run a Tcl script with tclsh first. That direct command makes the interpreter, script file, and arguments visible, which is useful when you need to diagnose a missing interpreter, a typo in the filename, or a script that exits with an error.

Install the Tcl interpreter

Tcl is the Tool Command Language. Tclsh reads commands from a file or standard input, so you need the Tcl package before a Tcl file can run. Tk is optional for a command-line script and only becomes necessary when your program opens a graphical interface.

On Debian or Ubuntu, install the Tcl package with apt. If you need a refresher on repositories, dependencies, and package updates, use the Linux package management guide before changing packages.

sudo apt install tcl

On Fedora, use DNF. On Arch Linux, Pacman installs the package named tcl.

sudo dnf install tcl
sudo pacman -S tcl

Run tclsh and enter puts [info patchlevel] to confirm that the interpreter starts and reports its version. The tclsh manual documents both interactive use and script-file execution.

Create a Tcl script with an argument

Save the following file as hello.tcl, where the first line lets Linux locate tclsh for direct execution and lindex reads the first command-line argument from argv.

#!/usr/bin/env tclsh
set name [lindex $argv 0]
if {$name eq ""} {
    puts stderr "usage: tclsh hello.tcl NAME"
    exit 2
}
puts "Hello, $name!"

The empty-string check prevents the script from printing a greeting with no name. It writes a usage message to standard error and returns exit status 2, so a calling shell or automation job can treat the missing argument as a failure.

Run the file with tclsh

From the directory that contains hello.tcl, call tclsh with the filename followed by the value for name. This route does not depend on executable permissions or the shebang, which makes it the best first command when you are checking a new script.

tclsh hello.tcl Ada
Terminal running tclsh hello.tcl Ada and printing Hello, Ada!
Run a Tcl file with tclsh

The command prints Hello, Ada! because Ada becomes the first argv item. Running the same file with no argument prints its usage line and returns status 2.

Make the file directly executable

Direct execution is optional and relies on the shebang plus the executable bit, so use it after the tclsh form works.

chmod +x hello.tcl
./hello.tcl Lin

If Linux returns Permission denied, inspect the file mode and ownership before changing anything else. The chmod and chown reference explains how the execute bit changes whether the shell can start a file.

Pass arguments and run external commands safely

Use the argv list when your script needs values from the command line. Tcl keeps each argument as one list element, so you can validate the values before passing them to another command.

set target [lindex $argv 0]
if {$target eq ""} {
    puts stderr "usage: tclsh show-home.tcl DIRECTORY"
    exit 2
}
exec ls -ld -- $target

Exec starts an external program and returns its standard output when the program succeeds. Passing ls and its arguments as separate words avoids building a shell command string, so spaces and shell metacharacters in a directory name are not interpreted by a shell. The official exec documentation covers its output and error behavior.

Use the Linux command-line primer for command arguments, then use the shell scripting and cron jobs tutorial when you need to choose between Tcl and a shell script for a larger automation task.

Check the common failure cases

  • Command not found means Tcl is missing or tclsh is not on your PATH. Install the Tcl package for your distribution, then open a new terminal.
  • Couldn’t read file means you are in the wrong directory or the filename differs from hello.tcl. Use pwd and ls before changing the script.
  • Permission denied after ./hello.tcl means the execute bit is absent or the filesystem blocks execution. Run it through tclsh while you inspect the file mode.
  • Usage: tclsh hello.tcl NAME is the script’s own validation response. Add the required name after the filename.

File permissions affect more than one language. The Linux users, groups, and permissions overview explains the ownership model behind the executable bit.

How do you run a Tcl script on Linux?

Run tclsh followed by the script filename and any arguments, such as tclsh hello.tcl Ada. This method works without making the file executable.

How do you make a Tcl script executable?

Put #!/usr/bin/env tclsh on the first line, run chmod +x hello.tcl, then start it with ./hello.tcl. Use tclsh hello.tcl first if you need to isolate an interpreter or permission problem.

Do you need Tk to run a Tcl script?

No. Install the Tcl package for command-line scripts. Add Tk only when the program needs the Tk graphical toolkit.

Keep tclsh as your first diagnostic command. Once that works, use direct execution only where a shebang and executable file make the task easier to run or schedule.