1

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 Sep 18 '18 at 14:01
1

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

X=5

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. – Psycho Punch Sep 19 '18 at 11:10
  • You have to call your invoker as . invoker.sh or source invoker.sh (depending on what fits your requirements better). – user1934428 Sep 20 '18 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? – Psycho Punch Sep 20 '18 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. – user1934428 Sep 20 '18 at 14:05
0

Here an example

#!/bin/bash

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

# Add here your code...
  • 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. – Psycho Punch Sep 18 '18 at 16:01

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.