How to keep commands quiet in TCL?

tcl
GIRI MURALI picture GIRI MURALI · Jul 26, 2013 · Viewed 9.9k times · Source

How to execute set command without printing output on the screen? I want to read a file without displaying the contents on the screen.

set a [open "giri.txt" r]
set b [read $ifile]

Answer

kostix picture kostix · Jul 26, 2013

What you're observing is just the standard behaviour of an interactive Tcl shell: each Tcl command returns a result value, and a return code. If the Tcl shell is interactive (that is, its input and output streams are connected to a terminal), after executing each command, the string representation of the result value the command returned is printed, and then the prompt is shown again. If the shell is not interactive, no results are printed and no prompt is shown.

(On a side note, such behaviour is ubiquitous with interpreters — various Unix shells, Python and Ruby interpreters do the same thing.)

If you want to inhibit such printouts in an interactive session (comes in handy from time to time), a simple hack to achieve that is to chain the command you want to "silence" with a "silent" command (producing a value whose string representation is an empty string), for instance:

set a [open "giri.txt" r]; list

Here, the list returned by the list command having no arguments is an empty list whose string representation is an empty string. In an interactive shell, this chain of commands will output literally nothing.

It bears repeating that such a hack might only ever be needed in an interactive session — do not use it in scripts.