1

I am using csh scripts for some automation purpose. I have a number of scripts, and the beginning of all of which I need to set a fixed set of environment variables.

|----SCRIPT 1---|     |----SCRIPT 2---|
|# set env vars |     |# set env vars |
|               |     |               |
|# do something |     |# do something |
|               |     |# else         |

So I thought that I'll put all the environment variables in another script and source it from each file. But as it turns out, environment variables set inside the sourced script are not reflected outside. Is there any clean way to achieve what I want? Or do I have to explicitly set environment variables in each script ?

UPDATE :

While sourcing the file, I was piping the output to a file using the 'tee' command. I noticed that after removing the pipe, the script file worked.

3
  • If you use source then it should work. If you actually execute the script it doesn't because that's a different process. What exactly did you do? Jan 19, 2016 at 13:44
  • I sourced the script.
    – johngreen
    Jan 20, 2016 at 7:09
  • You should post a minimal example that demonstrates the problem; because right now we can only guess as to where your error is... Jan 20, 2016 at 7:17

1 Answer 1

0

Yes, piping the output of source will run that part of the pipeline in a subshell, which means that any environment variables set by the sourcing of the other file will not affect the script's environment.

Example:

environment.csh:

setenv string "hello world"
echo 'The environment is now set up'

script.csh:

#!/bin/csh

source environment.csh | tee output
printf 'string is "%s"\n' "$string"

The output of running script.csh will be

The environment is now set up
string: Undefined variable.

Removing the pipe to tee will make it output

The environment is now set up
string is "hello world"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .