Basic commands in Tcl
In this part of the Tcl tutorial, we cover some basic Tcl commands.
The covered Tcl commands include the
The puts command
In the first example, we will mention the
puts command is used to print messages to the console or to other
channels like a file. The command has the following syntax:
puts ?-nonewline? ?channelId? string
puts is the command name. Optional parameters are
specified between question marks.
suppresses the newline character. By default, the command puts a newline
to each message. The
channelId must be an identifier for an open channel
such as the Tcl standard input channel
stdin, the return
value from an invocation of
socket. It defaults
to standard output channel
stdout if not specified.
string is the message to be printed.
#!/usr/bin/tclsh puts "This is Tcl tutorial" puts stdout "This is Tcl tutorial"
puts command prints a message to the console.
Both command invocations do the same thing.
The open command
open command opens a file, a serial port, or a command pipeline
and returns a channel identifier. In the following example, we use the
command to open a file.
#!/usr/bin/tclsh puts [open messages w] "This is Tcl tutorial"
puts command is used to write to a file, opened
for writing with the
$ cat messages This is Tcl tutorial
We show the contents of the messages file created by the above Tcl script.
The gets and flush commands
gets command reads a line from a channel, and the
flush command flushes buffered output of a channel.
In the following example we create a script that greets the user.
#!/usr/bin/tclsh puts -nonewline "What is your name? " flush stdout gets stdin name puts "Hello $name"
In this example, we request an input from the user and print the input in a custom greeting.
puts -nonewline "What is your name? "
-nonewline option suppresses the newline. The prompt
remains on the same line.
The output is buffered. To see the output immediately after the command
runs, we can use the
flush command. The
is the standard output. In our case it is a terminal; it is called a channel id
gets stdin name
gets command reads a line from the standard input. The
result is stored in the name variable.
puts "Hello $name"
Finally, we greet the user.
$ ./name.tcl What is your name? Jan Hello Jan
Running the example.
The incr command
incr increments the value of a variable.
It has the following syntax:
incr varName ?increment?
If a parameter is passed to the command, then its value is added to the value of the variable; otherwise the value is incremented by 1.
#!/usr/bin/tclsh # incr_cmd.tcl set x 5 incr x puts $x incr x 4 puts $x
The code example set a variable and increments it twice.
set x 5
Value 5 is set to the
incr x puts $x
x variable is incremented by 1. Number 6 is printed to
incr x 4 puts $x
Number 4 is added to the
x variable. The
prints 10 to the console.
$ ./incr_cmd.tcl 6 10
This is the output of the
The info command
info command returns information about the state
of the Tcl interpreter.
#!/usr/bin/tclsh puts [info tclversion] puts [info host] puts [info exists var]
The info command has several options. We show three of them.
puts [info tclversion]
Here we print the version of the Tcl interpreter.
puts [info host]
This line prints the host name.
puts [info exists var]
Finally we check if the variable var is set.
The set and unset commands
set command is used to create and read variables.
unset command destroys a variable.
#!/usr/bin/tclsh set x 23 puts $x puts [set x] unset x puts [info exists x]
An example showing the
set x 23
We create an
x variable and assign a value 23 to it.
We print the value of the
puts [set x]
This line also prints the value of the
set command with one parameter reads the value
of the variable. The value is passed to the
command and is printed to the terminal.
x is destroyed.
puts [info exists x]
We verify the existence of the variable using the
Command line arguments
Tcl scripts like any other scripts can take command line arguments. Tcl has three predefined variables.
- $argc - the number of arguments passed to the script
- $argv - the list of arguments
- $argv0 - the name of the script
#!/usr/bin/tclsh puts "The script has $argc arguments" puts "The list of arguments: $argv" puts "The name of the script is $argv0"
We use all the predefined variables in this script.
$ ./args.tcl 1 2 3 The script has 3 arguments The list of arguments: 1 2 3 The name of the script is ./args.tcl
Running the example.
This chapter covered some basics of the Tcl language.