2 ms·
One of these days I'm going to write a blog post about it... The built in "exec" command supports pipelines and file redirections with the usual syntax so you
by ufo 6y ago
One of these days I'm going to write a blog post about it...
The built in "exec" command supports pipelines and file redirections with the usual syntax so you can do
exec foo < bar | baz
and it works similarly to a bash command substitution
$(exec foo < bar | baz)
The main catch is that if you have an argument that is "|" or "<" then TCL interprets it as part of a pipeline, even if it came from a variable. However, the flip side us that you don't need to worry about arguments with spaces or asterisks. There is no word splitting or glob expansion so it is OK to leave everything unquoted.
set r "<"
set p "|"
exec foo $r bar $p baz
There are also a couple things that are slightly different from bash. One is that exec raises an exception if the command returns a nonzero exit code. If that is not what you want then you will need to use the TCL equivalent of try-catch. I wrote a small helper function for this common use case.
Another thing is that exec returns a string with the output of the command, similarly to $() in bash. If you want to output to stdout then you need to work around that.