0

I run bash scripts from tcsh shells. I want to set an environment variable in the tcsh shell from a bash script.

The method of doing this, if your parent shell is bash is to source a file, i.e., if I have a file called dog that contains

#!/bin/bash
myEnv=foo
export $myEnv

then from a bash shell, the command

. dog

will as expected create an envi source dog

then nothing happens. Is there a way to accomplish what I want to do?

thanks!

  • 1
    What does "will as expected create an envi source dog" mean in English? – waltinator Apr 13 '16 at 15:03
  • Sorry, that was a typo. Please see my response to @Serg below. My problem is that I can't find any way to communicate from within a bash script to the parent shell that is running in my xterm. All I need to do is to somehow create an environment variable in the parent tcsh shell from within a bash script. Seems like it should be really easy, rather than impossible. – Leo Simon Apr 14 '16 at 0:01
1

You want

export myEnv

not

export $myEnv
0

In addition to what Gunnar already noted, export will make the variable available only to that instance of bash shell and its child processes. In addition tcsh and bash syntax differ when it comes to variables.

I would suggest you set a variable in the syntax familiar to tcsh

eagle:~/sergrep> cat var_file.txt
setenv foo "TEST"
eagle:~/sergrep> source var_file.txt
eagle:~/sergrep> echo $foo
TEST
  • What you suggested @Serg works fine from tcsh. But my problem is that I need to run the source command from inside a bash script. That is somehow I have to make bash talk to my parent tcsh shell. When I run your var_file.txt from with a bash script, with the command . var_file.txt it returns an error saying that it can't find setenv. That's the essence of my problem. – Leo Simon Apr 13 '16 at 23:56
  • @LeoSimon talking to the parent shell is exactly the problem. To the best of my knowledge , there's no way to communicate a variable back to parent. May i ask what is the purpose of such behavior ? Is spawning a child tcsh from script an option ? export will make variables available to children – Sergiy Kolodyazhnyy Apr 14 '16 at 1:48
  • I'm pretty sure that if I could spawn a child tcsh script from within a bash script I'd be fine. The script would do exactly what you said above, i.e., simply setenv foo "TEST". The reason I want to do this that I want my latex scripts to read an environment variable. This variable has a default value. I have a complicated bash script, within which I sometimes want to specify a modified value for this environment variable. That's why I need to do a setenv from within the bash script. Hope this is clear enough? – Leo Simon Apr 14 '16 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.