[Semibug] Setting environment variables

BCLUG admin at bclug.ca
Wed Aug 9 19:37:49 EDT 2023


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



More information about the Semibug mailing list