1

So the bottom line here is that I am trying to have one go-to script work to set environment variables (let's call it setenv.sh) that will be used in subsequent scripts. In Windows (I know, that is a 4-letter word here...) I can use a batch file to set a environment variable for after the batch file is over. I am looking for that same effect.

However, I have just learned, through perusing the internet, that environment variables set in a script (aka a shell) only persist in child shells, and do not affect the parent shells. Okay whatever, well now what I want to know is can I run a script inline in another script? By inline, I am talking about something analogous to C/C++'s #include directive. I want this because if I can call a script as if it were running in the current script, my environment variables would at least persist through the duration of my desired script.

For example, something like this:

cat ~/install/setenv.sh | /bin/bash
echo "JBoss home directory is set to $JBOSS_HOME"

But I realize even that won't quite work, and I am starting a child shell with a call to /bin/bash. I am fine if I have to call this setenv.sh for each script I want it to run inline with.

4

if you want to include env variables, you can set those variables in a file, and then source that file in your script.

For example: in myconfig I could do:

#!/bin/bash
EDITOR=emacs
JBOSS_HOME=/path/to/jboss/home

then in my shell script:

#!/bin/bash
source /path/to/myconfig
echo "JBOSS home dir is $JBOSS_HOME" 

and that would work.

  • And a shortcut for source is just ., as in . /path/to/myconfig – psusi Mar 11 '14 at 22:08

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.