I have a shell script that sets some environment variables that I run through the source command to affect the current shell. I created another shell script that refers to this shell script and I thought calling source inside this other shell script would have the same effect as calling directly on the current shell, but apparently that's not the case. Is there anyway to tell this invoker script that whatever applies to the shell it's running in should be reflected to the current shell?

The script with the environment variables begin with set -a so I thought doing something like

$(cat env_script.sh)

would export everything to the invoking shell, but this too doesn't seem to work.

  • Can you set a different user for this script ? In that case, you can play with the .bashrc of this user.
    – Carpette
    Commented Sep 18, 2018 at 14:01

2 Answers 2


Your approach does not work for two reason.

First of all, let's assume that your env_script.sh contained a statement to set a variable, for instance


If you execute the statement

$(cat env_script.sh)

you are asking bash to run the content of this script as commands, i.e. to execute a program with the funny name X=5.

Hence, the correct way to bring the definitions into your shell would be

. env_script.sh

The second problem is that according to your writing, you want to propagate the variable to the "invoker script". Now it's not entirely clear what exactly you mean by "invoker script", but if this invoker script is not in turn sourcing the script you are about to write, but invoking it as a child process, there is technically no way to pass the variables directly to the invoker.

In short, if a shell needs variables to be set, and the settings are defined in a separate file, it needs a source command to import those.

  • The setup is this: I have 2 scripts; let's call them invoker.sh and env.sh. The latter contains a code that sets the variables, and the former invokes it through the source or . command. The problem is that when I run invoker.sh, the env variables don't propagate to the current shell. Commented Sep 19, 2018 at 11:10
  • You have to call your invoker as . invoker.sh or source invoker.sh (depending on what fits your requirements better). Commented Sep 20, 2018 at 7:20
  • What if I don't have the choice to source it as the invoker.sh is a system/user script in say $HOME/bin? Commented Sep 20, 2018 at 9:40
  • @PsychoPunch : What if you want to fly and don't have wings? :-D You could (inside invoker.sh) write the environment settings to a file and process the file in the calling script. Commented Sep 20, 2018 at 14:05

Here an example


CURR_DIR=$(dirname $(readlink -f $0))
. ${CURR_DIR}/set-env-script.sh

# Add here your code...
  • 1
    This is pretty much what I already do, and it doesn't work for me. Within the shell of the invoker script, the variables are set correctly, but when the control goes back to the current shell, the variables don't propagate. Commented Sep 18, 2018 at 16:01

You must log in to answer this question.

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