0

I have a script that sets some environment/global variables. It also has a function "deactivate ()" which undoes this sourcing.

source_script.rc

deactivate () {
env_var=$old_env_var
unset $old_env_var
}

old_env_var=$env_var
export env_var=new_value

I want to call deactivate from a program to undo the sourcing. However, when I try

other_program.sh

source /source/script
deactivate

It simply runs the deactivate function without exporting any of the variables to my environment. My question is, is there a way to source the deactivate function from another program so that it will call deactivate and actually set the necessary env variables?

  • 2
    Aside from the fact that your example does not show any **environment** variables, but just plain shell variables, the clean way would be IMO not to mix function definitions and variable definitions in the same file. If you want to have only a single file, put your variable definitions into a function `activate`, and after sourcing the file, call `activate` to set the variables and `deactivate` to unset them. – user1934428 Oct 27 '21 at 07:50
  • Right, so my question is, can you call activate/deactivate from another script rather than from the command line? Say I want some outside program to unset the env variables for me. I know I can source the file and run the function in the other script, but it doesnt set any of the env variables. –  Oct 27 '21 at 13:00
  • I do not understand what is going on. `Can set environment variables by sourcing a script from another script?` Yes, you can set environment variable by sourcing a script from another script. `It simply runs the deactivate function without exporting any of the variables to my environment.` How do you check it? What is "my environment"? `is there a way to source the deactivate function from another program so that it will call deactivate and actually set the necessary env variables?` What is "another program", is it `other_program.sh` script? Consult that "another program". – KamilCuk Oct 27 '21 at 13:51
  • Do you want just to do `source other_program.sh` from your interactive terminal? – KamilCuk Oct 27 '21 at 13:51
  • Sorry I must not be explaining clearly. I source the example above, source_script.rc. Now I have an env variable set: "env_var=new_value" which I can see by running "env". The old value of "env_var" is temporarily stored in the variable "old_env_var" so it can be reset by deactivate. I know I can run deactivate from the command line now to reset the env variables. –  Oct 27 '21 at 14:09
  • My question is, is there any way to call deactivate from another script (say other_program.sh) that will actually export the changes to my environment? When I add "source source_script.rc" followed by "deactivate" to other_program.sh it runs the deactivate function but does not change any env variables in my shell. –  Oct 27 '21 at 14:09
  • 1
    From what I understand... **1** you have a shell. **2** you have a script that sets variables and functions. **3** you can source that script in your shell to modify your shell environment. **4** if you have another script to deactivate variables you can source that one to do it. **5** But you cannot have an executable modify the env. variables from it's own environment, into your shell. **6** there is a way to do it, but it is not a proper, look at https://unix.stackexchange.com/questions/38205/change-environment-of-a-running-process#38212 – Nic3500 Oct 28 '21 at 02:42
  • @sdaniele : I don't understand your question about the difference between command line and script: You can call a bash**function** from any bash process where the function is definied, and _sourcing_ a file means that the function definitions in that file get defined in the sourcing process. – user1934428 Oct 28 '21 at 06:50
  • @Nic3500 great, thats the conclusion I came to, but just wanted to verify that was in fact the case. Thanks –  Oct 28 '21 at 15:12

0 Answers0