tclsh Command in Linux
The tclsh command in Linux starts the Tcl shell, which is an interpreter for running Tcl (Tool Command Language) scripts. It reads and evaluates Tcl commands from standard input or from a file. When invoked without any arguments, it enters interactive mode, reading commands from the standard input.
Table of Contents
Here is a comprehensive guide to the options available with the tclsh command −
- Syntax of tclsh Command
- tclsh Command Options
- Variables of tclsh Command
- Examples of tclsh Command in Linux
Syntax of tclsh Command
The syntax of the tclsh command in Linux is as follows −
tclsh [-encoding name] [fileName] [arg]
In the above syntax, the tclsh command invokes the Tcl shell.
tclsh Command Options
The description of other options in the syntax is listed below −
Option | Description |
---|---|
-encoding name | Specifies the encoding of the script file. |
fileName | Specifies the name of the Tcl script to run. |
arg | Optional arguments passed to the Tcl script, available via argv and argc. |
Variables of tclsh Command
The predefined variables of the Linux tclsh command are listed below −
Variable | Description |
---|---|
argc | Number of arguments passed to the script, excluding the script name. |
argv | A list containing the arguments passed to the script. |
argv0 | The name of the script file or the name used to invoke tclsh. |
tcl_interactive | Set to 1 if running interactively, 0 otherwise. |
These variables can be useful in scenarios where handling script arguments, identifying the environment, or managing script behavior based on the number of arguments passed is required. Here is a short description of each scenario −
argc − Checks if the script has received any arguments and if not, displays a usage message.
if { $argc == 0 } {puts "Usage: tclsh script.tcl <arg1> <arg2>"}
argv − Loop through all passed arguments and process them.
foreach arg $argv { puts "Processing argument: $arg" }
argv0 − Displays the name of the script or command used to invoke the script.
puts "This script was called using: $argv0"
tcl_interactive − Checks if the script is running interactively and provides a prompt.
if { $tcl_interactive } { puts "Welcome to the interactive Tcl shell!" }
Examples of tclsh Command in Linux
This section explores how to use the tclsh command in Linux with examples −
- Invoking the tclsh Shell
- Running a Tcl Script from a File
- Running a Tcl Script with Arguments
- Running a Tcl Script with a Specific Encoding Type
Invoking the tclsh Shell
To invoke the tclsh shell, use the command without any option −
tclsh

The above command initiates the tclsh shell where the Tcl command can be executed. For example, to set a variable, use the set command with the variable name and value −
set var "tutorialspoint"

To display the value of the variable, use −
puts $var

Similarly, to perform an arithmetic operation use −
set x 5 set y 10 puts [expr {$x + $y}]

A list of commonly used Tcl commands is given below −
Command | Description |
---|---|
puts | Print output to console |
set | Set a variable |
expr | Evaluate expressions |
if | Conditional logic |
while | Loop while a condition is true |
for | Traditional for loop |
foreach | Loop over a list |
break | Exit a loop |
continue | Skip to next iteration |
proc | Define a procedure (function) |
To exit the shell, type exit and press Enter −

Running a Tcl Script from a File
To run the Tcl script from a file, use the tclsh command with the script name −
tclsh myscript.tcl

The output image shows the Tcl script and its execution, using the tclsh command −
Running a Tcl Script with Arguments
To run a Tcl script with arguments, use the tclsh command in the following way −
tclsh myscript.tcl arg1 arg2
The output image shows the Tcl script and its execution, using the tclsh command −

In the Tcl script, the argc variable is used, which checks the number of arguments.
Running a Tcl Script with a Specific Encoding Type
To run a Tcl script with a specific encoding type, use the tclsh command in the following way −
tclsh -encoding utf-8 myscript.tcl arg1 arg2
Note that specifying the encoding type is an optional step.
Conclusion
The tclsh command in Linux is used to start the Tcl shell, an interpreter for executing Tcl scripts. It allows running commands interactively or from a script file, with options to specify encoding and pass arguments to the script. Key features include predefined variables like argc, argv, and tcl_interactive, which help manage script inputs and interactions.
The tclsh command supports a variety of Tcl operations, such as setting variables, performing arithmetic, and defining loops or functions.