Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
redirect the output of a tcl command to a variable

Posted by deepshikha_dutta (deepshikha_dutta), 8 October 2007

I have written this code to redirect the output of a tcl command to a variable . But it is not working.

Facing problem with:
"interp alias {} puts {} ::forward::r_puts"

rename puts t_puts
interp alias {} puts {} t_puts

namespace eval forward {

proc forward::r_puts { args} {
 variable result
       append result [lindex $args 1]

proc forward {{args} args1} {
set tee 0
set append 0
set file_name "[lindex $args 0]"
set script "$args1"
   set prev_alias [interp alias {} puts]
   interp alias {} puts {} ::forward::r_puts
   set ::forward::result {}

 catch {uplevel $script} rsl
 eval {interp alias {} puts {}} $prev_alias

   upvar $file_name my_variable

     set my_variable $::forward::result

 return $rsl

Command to execute
tcl>forward {out_variable} {ls}

Expected output :
tcl> puts $out_variable
The data in the variable should be the stdout of the command "ls"
Note: "ls" is a tcl command .

Posted by admin (Graham Ellis), 8 October 2007
I think you need something as short as

set varname [billy param1 param2 etc]

In this example, a variable named "varname" is set to the result of running the billy command with three parameters - param1, param2 and etc

Posted by deepshikha_dutta (deepshikha_dutta), 8 October 2007

I could not able to find this command it says that command not found.

tcl>billy ls
Error: invalid command name "billy"


Posted by admin (Graham Ellis), 8 October 2007
Yes, it would - billy is an example - in other words it's put in there to illustrate the syntax you would use.  I don't know what command you want to run - you will need to use its name in place of billy.

Posted by deepshikha_dutta (deepshikha_dutta), 8 October 2007

I need to run command "ls" .

set var [ls]  

puts $var is still giving empty string as it should not be.

Please let me know how this will work.
rename puts tcl_puts
interp alias {} puts {} tcl_puts

namespace eval redirect {
proc var_puts { args} {
tcl_puts "$args"

proc redirect {args} {
   set prev_alias [interp alias {} puts]
 interp alias {} puts {} ::redirect::var_puts

uplevel $args
 eval {interp alias {} puts {}} $prev_alias


Note: I want to send the output of "uplevel $script" as argument to  "::redirect::var_puts "

Note: $args in uplevel can be any command.


Posted by admin (Graham Ellis), 8 October 2007
I really worry about the code and direction you're headed - there has to be an easier way.  Let's start by establishing why you need to use "ls" - which will work intergctive at the command line but not with ina script as it works via the unknown interface.    It looks to me like you may be trying to get a list of all the file names in a diectory.   In which case, what is wrong with

set water_biscuit [glob *]

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho