[Semibug] Setting environment variables

Joe Price jprice at spacewalk.net
Thu Aug 10 12:11:30 EDT 2023


Just dealt with this situation this week..

Have a look at https://stackoverflow.com/a/45971167

This is essentially what I did in bash: set -a && source env_vars.sh && 
set +a


On 8/9/2023 7:37 PM, BCLUG wrote:
> Mark Moellering wrote on 2023-08-09 13:24:
>
>> I am not sure what you mean by "test your script in a subshell" ...
>
> Usually a sub-shell is running something inside brackets, i.e.
>
> in the following example, the code inside brackets is slightly 
> isolated from the `pwd` that runs afterwards:
>
>> $ (cd subshell-test/ ; pwd); pwd
>> /tmp/semibug/subshell-test
>> /tmp/semibug
>
>
>> I am more of a programmer than an admin.
>>
>> Here is my script:
>>
>> -----------------------------------------------------
>>
>> #!/bin/sh
>>
>> GODADDY_KEY="xxxxxx"
>> export GODADDY_KEY
>>
>> GODADDY_SECRET="yyyyyy"
>> export GODADDY_SECRET
>>
>> -----------------------------------------------------
>>
>>
>> The script runs and exits cleanly but if I run "env" afterwards, the 
>> environment variables are not set.  I have no idea what I am doing 
>> wrong.
>
> Code looks good, but it may be in how you're invoking it.
>
> If you `source` the script (one can use a `.` instead of the word 
> `source` to run a script), then I believe the ENV vars ought to 
> persist when the script finishes.
>
> If you set the shell to executable, or invoke it via `sh 
> my_script.sh`, then that would (probably) count as a sub-shell and the 
> ENV vars won't persist.
>
>
> Hope that helps,
>
> rb
>
> _______________________________________________
> Semibug mailing list
> Semibug at lists.nycbug.org
> https://lists.nycbug.org:8443/mailman/listinfo/semibug



More information about the Semibug mailing list