Exported variables not available [duplicate] - linux

This question already has answers here:
Setting console env using a shell script
(1 answer)
Defining a variable with or without export
(15 answers)
Closed 9 months ago.
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?

Related

Difference between running a script and copying its content and running it on in a terminal [duplicate]

This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 2 years ago.
I have a seemingly simple bash script to setup my environment:
The first two lines are:
#!/bin/bash
export CVE_ENV_DIR=$PWD
easy, hey? Well, see what happens when I run it, I get the following output:
$ echo $PWD
/work/env
$ ./env.sh
$ echo $CVE_ENV_DIR
$
Why does CVE_ENV_DIR not get set to /work/env? What is happening here? When I type export CVE_ENV_DIR=$PWD manually on the shell, it works as expected...
Child shells cannot affect the environment of their parent. If you want the script to affect the parent's environment, you need to:
source ./env.sh
So what's going on? When you run a bash script, either by bash env.sh or env.sh, you're spawning a program with its own environment, an environment that's divorced from its parent. But, when you run the commands contained in the script at the command line, or using source, there is no spawned environment.
Edit to address #syme's comment. Bash scripts meant to be read using source are often pure configuration, containing only assignments and calculations. It's possible to also make them a little more helpful and self-documenting with a clever she-bang hack like:
#!/bin/echo USAGE: source
# Default configuration file for the Frobnicator package.
FOO=bar
BAR=$(stat /baz)
[[ -f /baz ]] && BAZ=file || BAZ=
export FOO BAR BAZ
Making a bash script meant for configuration look like a configuration script, you help future maintainers. You also help yourself my modularizing your script code into distinct parts, each part with its one unique function.
As a side note, please don't export on the same line as you assign.

Bash script output not showing up during execution? [duplicate]

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have created a virtual environment on my debian system and i made a script that activates it (should).
However when i execute the script nothing shows up, not even an error, my guess is that it is running in a different shell or something but I don't know how to solve it.
Here is the code of the script
#!/bin/bash
source ~/PythonEnv/environments/my_env/bin/activate
I have changed the permissions already with chmod u+x, so that is not a problem.
When i execute the script nothing shows up at all. Any thoughts???
Add set -x at the beginning of your bash script will do the trick.
-x Print commands and their arguments as they are executed.
You can see more bash options here
http://linuxcommand.org/lc3_man_pages/seth.html
Adding x-permissions is not necessary, since you are using source with an absolute path. Of course this sets the environment only which is executed by the shell script which you have posted here. If you want the changes in your interactive shell, it is pointless to do it inside a script. You have to source the activate script in your shell (respectively inside that process where you want the environment to be modified).

System environment variables not accessible in bash script

I'm running Ubuntu on Windows Subsystem for Linux, and I have a bash script that I want to run that needs to access system environment variables. Specifically, I defined an environment variable on my system with the following command:
export SLACK_LEGACY_API_TOKEN="Insert Token Here"
I then define a file called slack.sh that looks like this:
#!/usr/bin/env bash
echo $SLACK_LEGACY_API_TOKEN
I then run this script with source ./slack.sh. When I run this command, it just prints out a blank line - it's not getting the value of the environment variable.
I've tried different syntax in the .sh file for referencing the environment variable, like "$SLACK_LEGACY_API_TOKEN" and ${SLACK_LEGACY_API_TOKEN} but same result. I've also run the script with /slack.sh and . ./slack.sh but same results.
How do I get my script to see $SLACK_LEGACY_API_TOKEN?
As mentioned by #chepner in the comments within my question, the issue was caused by defining the .sh file in my Windows environment. This results in the file being saved with DOS line endings which was causing the issues with the script. Redefining the file entirely within the Linux environment solved the problem.

environmental variable not available for next command in shell script [duplicate]

This question already has answers here:
Shell script to set environment variables
(5 answers)
Closed 6 years ago.
We have a shell script named source.sh that exports many ENV variables required for a build.
I am trying to access those ENV variables by running source.sh in my script, but even after running that I am not able to access ENV variables for the succeeding commands.
My Script look alike:
#!/bin/bash
sh source.sh
cd $ROOT_VOS
make
But here cd $ROOT_VOSis not happening
sh source.sh
...runs source.sh in a separate copy of sh (actually, since your parent shell is bash and the child is sh, it's not just a "separate copy", it's an entirely different interpreter). When that separate interpreter exits, local variables and other changes to process-local state are gone. (Environment variables are inherited by child processes; they aren't propagated up the tree to parents).
Instead, use:
# POSIX-compliant: execute every command in source.sh in the current shell
. source.sh
...or...
# bash-specific, perhaps more-readable, synonym for the same
source source.sh

How to make declare in a Linux shell script?

I want to put below declare in a shell script: proxy_set
declare -x https_proxy="https://192.168.220.4:8080/"
And then I execute it like below.
$ ./proxy_set
But "export" shows nothing happened.
And in another way if I execute it like this:
$ source proxy_set
Then "export" shows it works!
My question is how can I make it work without additional "source" cmd?
Thanks!
You can't. Setting variables in the environment only affects the environment of that shell and any future children it spawns; there's no way to affect the parent shell. When you run it without the source (or .), a brand new shell is started up, then the variable is set in that shell's environment, and then that shell exits, taking its environment with it.
The source reads the commands and executes them within the current shell as if you had typed them.
So if you want to set environment variables in a script, you have to source it. Alternatively, you can have a command generate shell commands as output instead of running them, and then the parent can evaluate the output of the command. Things like ssh-agent use this approach.
Try just adding:
export https_proxy="https://192.168.220.4:8080/"
Then execute your script normally.

Resources