0

TCL script

set ::env(foo) "bar" 

bash script

echo ${env(foo)} echo $foo 

I am able to print the environment variable in the TCL script but for some reason I can't print the environment variable in my bash script. What am I doing wrong?

3
  • 2
    What is the relationship between the two scripts? Commented May 11, 2021 at 17:09
  • If foo is in the environment of your shell, it's simply echo "$foo". On startup, the shell simply defines a regular shell variable for each name in the environment that forms a valid shell identifier. Commented May 11, 2021 at 17:52
  • 2
    Following up Shawn's comment: if you're invoking the Tcl script from your shell, and you expect the env var to appear in your shell, it can't happen -- a child process is not allowed to alter the environment of its parent. Commented May 11, 2021 at 18:40

1 Answer 1

2

If your bash script contains something like this (let's call it showenv)

#! /bin/bash echo $foo 

and your tcl

#! /usr/bin/tclsh set ::env(foo) bar exec ./showenv >>& /dev/tty 

then, showenv will show

bar 

that is, the tcl script sets the environment for its children processes.

Sign up to request clarification or add additional context in comments.

1 Comment

Environment variables never automatically go the other way, from child to parent. Instead, one has to write something (to standard out or to a file) that the caller interprets as an instruction to set an environment variable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.