output.txt, the output file is empty. But running tclsh > output.txt and typing set x 2 in the console makes it work." /> output.txt, the output file is empty. But running tclsh > output.txt and typing set x 2 in the console makes it work." /> output.txt, the output file is empty. But running tclsh > output.txt and typing set x 2 in the console makes it work."/>

tclsh command output isn't printed when using input redirect

41 views Asked by At

If I run echo "set x 2 | tclsh > output.txt, the output file is empty. But running tclsh > output.txt and typing set x 2 in the console makes it work.

My guess is that tclsh sees that I'm using input redirection (and thus the session isn't interactive), and doesn't bother to send return values to stdout. Any ideas to get it to print anyway?

1

There are 1 answers

0
Donal Fellows On

The printing of command results is strictly a feature of the interactive mode of tclsh (and wish) and isn't formally considered a language feature. Tcl determines that by looking at whether input is coming from a terminal, and sets the tcl_interactive to a boolean value accordingly; the tclsh REPL sees that and prints results when it is true. Pipes aren't terminals or consoles.

The usual recommendation is that if you want something printed, be explicit and print it yourself:

echo 'puts [set x 2]` | tclsh >output.txt

And if your script is getting long enough that you're thinking about a heredoc, put it in its own file. It's just so much easier that way.