Simple Tcl Commands

Some simple commands are explained here so they can be used for examples in following pages.

The set Command

The set command is used primarily to assign values to variables. It is used like this: set variable value

If a value is given, set returns that, otherwise set returns the current value of variable.

An example of set

Assign the value "bar" to variable foo: set foo bar This returns "bar".

The puts Command

The puts command is mostly used to write to files, but it defaults to stdout so can be used for primitve output.

It is used like this: puts string file where file defaults to stdout.

An example of puts

puts foo outputs: foo

The gets Command

This commands allows reading from files. It reads one line at a time and chops off the terminating newline.

It is used like this: gets file ?varName? If varName is given gets sets varName to the line just read and returns the number of characters read, otherwise it returns the line read. It returns -1 for EOF.

An example of gets

Reading from stdin: gets stdin variable

The expr Command

The expr command is a complex command that knows all about mathematical stuff: expr 4 + 3 * 2 returns 10.

expr knows about functions like abs, ceil, round etc. expr sqrt(36) returns 6.


Previous Contents Next
Clinton Roy