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?
The printing of command results is strictly a feature of the interactive mode of
tclsh(andwish) and isn't formally considered a language feature. Tcl determines that by looking at whether input is coming from a terminal, and sets thetcl_interactiveto a boolean value accordingly; thetclshREPL 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:
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.