Passing Parameters between Tcl Scripts


I have two Tcl scripts running under Linux: script1.tcl launches script2.tcl. I like to pass all command-line parameters (that is, $argv) from script1.tcl to script2.tcl and then some. However, the $argv parameter arrives to script2.tcl were groupped into one token instead of separate tokens. An example will make it clearer:

Let say, I invoked script1.tcl like this:

tclsh script1.tcl –file config.txt –userCount 5

Then in script1.tcl, I see that $argv is broken into the following tokens:


This is the behavior I want. Now, in script1.tcl, I spawn the second script, I pass along $argv along with some other parameters, like this:

exec tclsh script1.tcl $argv –userIndex 0

Then in script2.tcl, $argv’s token are broken down as shown:

–file config.txt –userCount 5

Notice that the first token is everything that I passed into script1.tcl and Tcl group them together as one token instead of separate ones. This is not the behavior I am looking for so I set out to solve it.


After a bit of searching around, I found a solution from the Tickler Wiki. Basically, instead of one exec line, I have to do this:

set cmd [list exec tclsh script2.tcl]; # Construct the command line
set cmd [concat $cmd $argv –userIndex 0]; # append the arguments
if {[catch $cmd result]} {
puts “Exec failed with error: $result”
} else {
puts “Exec passed, output:”
puts $result

This solution works because the concat command in the second line broke $argv into pieces and concatenate it to $cmd. While this solution works, it is a bit of a hack. Without detailed comments, I will not know what to do with it six months from now. If you have a different (or better) solution, please post your comments here.


2 thoughts on “Passing Parameters between Tcl Scripts

  1. Amitesh

    How to pass data between tcl scrips?

    a snippet from script 1:-

    exec run1.tcl hello me you

    a snippet from script 2:

    set ar $argc
    puts $ar

    i am not getting any output from script 2.

    Kindly suggest.

  2. Mystc Odin

    in Tcl 8.5 you can use:
    exec tclsh script1.tcl {*}$argv –userIndex 0

    The {*} expands a list variable instead of putting it as a single list element

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s