Basic commands in Tcl
In this part of the Tcl tutorial, we will cover some basic Tcl commands.
In the first example, we will mention the puts command.
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
The puts is the command name. Optional parameters are
specified between question marks. The -nonewline switch
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 open or socket. It defaults to stdout, if not specified.
Finally the string is the message to be printed.
#!/usr/bin/tclsh puts "This is Tcl tutorial" puts stdout "This is Tcl tutorial"
The puts command prints a message to the console.
Both command invocations do the same thing.
#!/usr/bin/tclsh puts [open messages w] "This is Tcl tutorial"
Here we use the puts command to write to a file.
We open a file for writing using the open command.
$ cat messages This is Tcl tutorial
We show the contents of the messages file created by the above Tcl script.
Greeting a 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? "
The -nonewline option suppresses the newline. The prompt
remains on the same line.
flush stdout
The output is buffered. To see the output immediately after the command
runs, we can use the flush command. The stdout
is the standard output. In our case a terminal. It is called a channel id
in Tcl.
gets stdin name
The 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 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 command is used to create and read variables.
The 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 and unset commands.
set x 23
We create an x variable and assign a value 23 to it.
puts $x
We print the value of the x variable.
puts [set x]
This line also prints the value of the x variable.
The set command with one parameter reads the value
of the variable. The value is passed to the puts
command and printed to the terminal.
unset x
The variable x is destroyed.
puts [info exists x]
We verify the existence of the variable using the info exists
command.
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.