I think there is probably a simple answer to this question, but I have been unable to come across it yet; I am trying (unsuccessfully) to compute a value in the ROOT environment, set a variable to that value, and then use that variable in the shell environment.
I read in the ROOT Users’ Guide that shell commands can be executed by doing .! your_command_here, but I am experiencing limited success with this method. For example, in the ROOT environment, I can do:
.! “echo $PATH”;
just fine, but when I do:
.! set this_var=“any_value”;
.! echo $this_var;
it doesn’t output anything; what’s more, when I quit ROOT and do:
in the shell, I get: this_var: Undefined variable.
To restate my aims, I would want this_var to be equal to a value obtained in the ROOT environment.
I tried using gSystem->Setenv(“ZZZ”,“100”) inside ROOT. as suggested by Philippe.
The code executed without problem, but the the envinronment variable “ZZZ” does not exist in the Linux terminal after exiting ROOT.
I was hoping to use ROOT to set an environment variable in ROOT and use it after exiting, (while still in the same Linux terminal that ran ROOT).
I was using Root V5.26
environment variables are local to a process; they get passed down to sub-processes. ROOT doesn’t provide a means to set the environment variable of a parent process. Please search the web to find out whether it’s possible at all and how it’s done.
[quote]I was hoping to use ROOT to set an environment variable in ROOT and use it after exiting, (while still in the same Linux terminal that ran ROOT). [/quote]As far as I know, this is actually technically not possible. There is no way (that I know of) for a sub-process to affect the environment of the caller process. In particular you will notice that any ‘shell script’ that is supposed to set variable is either embedded in the starting script or ‘sourced’ (in bash you do ‘. mysetter’ and in csh you literally do ‘source mysetter’).
One way to emulate would be use (tcsh syntax used here):
source `root.exe -b -l -q myscript.C`where myscript would write a temporary file (let’s say /tmp/myenvsetter.sh). The last line of the file should delete the file (rm -rf /tmp/myenvsetter.sh). The last thing myscript.C should do is write to stdout the name of this temporary file.