Use tcl variables in a shell script

I have a tcl file with some variables set. I want to source this tcl file into my shell script to use them there.

When I do source <filename>.tcl, and echo the variable, it complains saying variable not found. Any help would be appreciated.

Upvotes: 0

Views: 1542

Answers (2)

glenn jackman
glenn jackman

Reputation: 246877

It's ... convoluted

Here's Tcl script that sets a variable:

$ cat > vars.tcl
set var "this is a Tcl value"

Let's see if we can get Tcl to output that in shell syntax:

$ echo 'source vars.tcl; foreach _v {var} {puts "$_v=\"[set $_v]\""}' | tclsh
var="this is a Tcl value"

So far so good. Now, with bash:

$ echo "${var:-var is unset}"
var is unset
$ . <(echo 'source vars.tcl; foreach _v {var} {puts "$_v=\"[set $_v]\""}' | tclsh)
$ echo "${var:-var is unset}"
this is a Tcl value

with if you're using plain /bin/sh

$ echo "${var:-var is unset}"
var is unset
$ . <(echo 'source vars.tcl; foreach _v {var} {puts "$_v=\"[set $_v]\""}' | tclsh)
sh: 1: Syntax error: "(" unexpected
$ eval "$(echo 'source vars.tcl; foreach _v {var} {puts "$_v=\"[set $_v]\""}' | tclsh)"
$ echo "${var:-var is unset}"
this is a Tcl value

This assumes that your Tcl script does nothing beyond setting variables, or that you're OK with sourcing it to get the variables set; and the variable values do not contain double quotes.

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386020

You cannot use tcl variables in a shell script. Even though source seems to work, it is not going to do what you think it does. When you use the source command from a shell script, it will attempt to interpret the contents of the file as a shell script.

Upvotes: 0

Related Questions