3

I'd like to source a file inside a tcsh script. In code:

#!/bin/tcsh

unsetenv LD_LIBRARY_PATH
source $1    
echo $LD_LIBRARY_PATH > temp_file

The expected result: The environment variable set by the script using the source command will be printed into a temporary file.

Actual result: The sourced file runs nicely, but the LD_LIBRARY_PATH variable remains empty.

Any ideas on how to get this to work?


P.S. tcsh is of course not my shell of choice, but I have a script at work that sets up numerous environment variables in tcsh that I want to get working in bash. I figured the easiest way (other than rewriting the tcsh shell every time) is to source it, print out the variables into a file, and with a bit of shell magic, re-source everthing back in bash.

  • If you run env before and after source, does anything change? If not, try the same inside the sourced script. That might give some clues. – ams Apr 12 '12 at 11:55
  • 2
    I just tried it; it works for me the way you were expecting. Are you sure your sourced script is setting LD_LIBRARY_PATH? You can add a line at the very end (or anywhere the script might exit) that echos it to a different temp file. – Shawn J. Goff Apr 12 '12 at 12:02
  • Post the sourced script (preferably shrunk to a small example that exhibits the problem). – Gilles 'SO- stop being evil' Apr 12 '12 at 23:20
1

I've tried with csh on OpenBSD. I guess my example works also with tcsh.

File test.csh:

#!/bin/csh
source data.csh
echo $VARIABLE

File data.csh

#!/bin/csh
setenv VARIABLE 1234

Result:

$ csh test.csh
1234
1

For further reference, this does indeed work, but one should not use the shebang to the tcsh path:

 #!/bin/tcsh 

but rather use the path to 'env':

 #!/usr/bin/env tcsh

to allow flexibility on systems with tcsh installed in different paths. For example, if 'tcsh' is installed in '/usr/bin/tcsh' then the former will fail, while the latter will still work.

  • 1
    Please elaborate. Why did this help? – Zvika Nov 24 '17 at 10:47

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.