I am working on automating some existing processes through linux shell scripts. What I want to do is run a script remotely through SSH that will then complete a number of tasks on the remote host.
The current file structure is like this:
setup.sh -> exports a list of environment variables
automate.sh -> executes setup.sh then runs automation
Let's say setup.sh does "export VAR=/test" and at the end of the automate.sh script I put the following line "env | grep VAR".
After unsetting VAR and then executing automate.sh, the variable does not get output, meaning it is empty.
Setting the environment variables in .profile or .bashrc will not work for me as setup.sh dynamically sets environment variables based on my parameters (For example, executing code from different regions requires different variables to be set, but all variables have the same name so the same code can be used for all regions).
I am assuming the problem here comes from automation.sh creating a new shell, then setup.sh creating a new child shell, meaning that the exported variables are not available in automation.sh
Can anyone with more experience tell me if this is likely the case? Or how I would verify if this is the case or not? Any suggested solutions would be welcome too.
Thanks.
Update: A suggested solution was to source setup.sh instead of executing it. This solves the problem. However, it would then mean I would need to source setup.sh in multiple places as other scripts will be executed later after automation.sh has complete.
Is there any way to run setup.sh and have the exported variables available to all following scripts?
setup.shor does it source it? And when testing this locally, did youunset VARbefore executingautomate.sh(just to be sure it was not already set beforehand)?callis not clear). Next, instead of executingsetup.shtry to source it:source setup.shor. setup.sh(dot + space +setup.sh). The former sets the environment variables in a child shell only. The latter sets them in the context of the current shell.setup.shfrom your other shells and usessh remote '. setup.sh; ./automation.sh; ./someother.sh...'