0

I wish to write a script for users on my server to activate Anaconda. Now the documentation suggests I can activate the installed software environment afterwards by

eval "$(<path to anaconda>/bin/conda shell.bash hook)"

Other clues suggest that I can also activate it by

source <path to anaconda>/bin/activate

My problem is both approaches seem to work fine in the terminal (bash), but if I try to do it inside a bash script so as to provide my users an easy way to activate Anaconda, it has no effect outside that script. I am a bash novice, so I guess it is just me who does not know what to do. I am expecting to be able to do something like:

export <effects of eval'ing or source'ing some file>

But I only know how to export specific variables.

Thomas Arildsen
  • 1,079
  • 2
  • 14
  • 31
  • 1
    What exactly do you think `export` does? If you think it has an effect on the caller, you are incorrect. – William Pursell Apr 15 '19 at 12:25
  • No UNIX process can change its parent process's environment variables using well-supported and portable interfaces, *period*. The whole point of sourcing a file is to run it in your *current* process. If you execute a script without sourcing it, it's in a different process, so it *can't possibly* change variables in your original shell. – Charles Duffy Apr 15 '19 at 12:28
  • That's why when you're running `ssh-agent`, you need to (yourself, as the user!) make it `eval "$(ssh-agent)"`, to evaluate the assignments it wrote to stdout as script text. If there were a way for them to write it where you wouldn't have to do that, don't you think they would have? – Charles Duffy Apr 15 '19 at 12:30
  • This is a feature: It means users can run commands without needing to worry about what changes are being made by those commands to transient state, unless they're explicitly run in a way that *permits* those commands to change the user's interactive shell's state. (That's especially important when a command runs with a different privilege level from the user who invoked it!) – Charles Duffy Apr 15 '19 at 12:32
  • ...so, if you want your script to change shell state? Your users should *source* it, not *run* it. – Charles Duffy Apr 15 '19 at 12:35
  • @CharlesDuffy If I knew there was no way to write a script to abstract the dirty details of setting environment variables away for users that may not know and do not have to know all the details, do you think I would have asked? – Thomas Arildsen Apr 15 '19 at 15:06
  • 1
    So, I worked around this now by simply invoking bash in a script after sourcing the necessary file. Works fine for my purpose. I guess someone could have mentioned that, but instead the focus seemed to be on blaming me for not knowing that I could not do it exactly the way I thought it might be possible and for posting duplicate questions – Thomas Arildsen Apr 15 '19 at 15:19

0 Answers0