0

I'm trying to run a zsh/bash script that writes several values to environment variables, I want these variables available to the parent shell as they are required for several tools we use. If I manually run the script using '. myscript myparamater' I get the expected result, however defining a zsh function to do this without having to use dot notation does not result in the variables being set.

I'm pretty new to zsh/bash scripting, and this has been my first real effort at writing something useful. I am running this on MacOS but would like for it to work in Linux as well, the script my function is sourcing is doing some bash logic and in some cases also executing a third-party executable (really a bash script that calls a java binary). In my script I'm calling the third-party tool directly using its executable name, calling it using exec or dot notation does not seem to work properly.

zsh Function:

function okta-auth {
    . okta_setprofile $1
}

My script: https://gist.github.com/ckizziar/a60a84a6a148a8fd7b0ef536409352d3

Using '. okta_setprofile myprofile' I receive the expected output of the okta_setprofile script, four environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION, and AWS_SESSION_TOKEN) are set in my shell. Using 'okta-auth myprofile' the script feedback is the same as previously, however after execution, the environment variables are not set or updated.

Updated 20190206 to show flow

okta_setprofile flow diagam

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • If you invoke your function `okta-auth`, no subshell is created. – user1934428 Feb 05 '19 at 13:38
  • If you have program A running with environment E1 and it calls program B, then it is tricky to have B set A's environment to E2 without cooperation of A. cf. https://stackoverflow.com/questions/205064/is-there-a-way-to-change-the-environment-variables-of-another-process-in-unix – jhnc Feb 05 '19 at 16:01
  • 1
    @jhnc I can understand that, but is that applicable in this case? I'm likely misunderstanding the path or any forks it may be taking. My script itself contains several if statements that decide whether or not it needs to run another command to refresh, but in the end the script is setting the environment variables. I thought perhaps the fact that it might run other commands might knock it into another shell and not come back properly, but in that case I would expect to have the same result calling it outside of the zsh function, but calling the script directly '. okta_setprofile myname' works. – Chris Kizziar Feb 05 '19 at 18:12
  • Second sentence of you question seems to say you want B's E2 to become A's E1. Perhaps describe the tree of processes you are creating and how you want their environments to interact. – jhnc Feb 06 '19 at 00:00
  • Fair enough @jhnc - It won't let me embed and image but I added a flow diagram link to the question, hope that helps. My apologies if I'm not able to clarify sufficiently. Essentially, the script okta_setprofile is exporting the environment variables as the last item in the script. This works when invoking the script using '. okta_setprofile profilename' from the command line, but not when invoking a zsh function that makes the same call '. okta_setprofile profilename'. Hence my assumption that the zsh function was itself spawning a subshell. – Chris Kizziar Feb 06 '19 at 13:35
  • I can't reproduce what you describe. You mention `bash` - where does `bash` come into play? – Armali Feb 07 '19 at 12:26
  • 1
    @Armali Sorry your comment flew under my radar, the only time bash comes into play is when the okta_setprofile script has to execute the 'withokta' command. That third-party command is essentially a bash script calling a Java app. Aside from that everything should be staying in zsh. – Chris Kizziar Feb 11 '19 at 18:18

0 Answers0