bash environment variables not getting exported [duplicate] - bash

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 7 years ago.
I have two shell scripts:
First : 1.sh, it reads as follows:
export PROP="my val"
Second : 2.sh, it reads as follows:
./1.sh
echo $PROP
Both have execute permission. When I run 2.sh, I am expecting that environment variable PROP set and exported by 1.sh, is visible to echo $PROP statement in 2.sh and would get printed. But output is blank indicating that PROP is not visible to 2.sh. What is wrong here?

Try sourcing the script in the current process:
. 1.sh
echo $PROP
And you can then drop export altogether:
PROP="my val"
The problem is that you're running 1.sh in a new shell process, so any changes it makes to its environment are lost when the process ends. Specifically, export makes the variable available to children of the current process, so in this case it cannot affect 2.sh (the parent process).

Related

Trying to export a bash variable from an .sh file to another file that runs it [duplicate]

This question already has answers here:
Source files in a bash script
(2 answers)
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 2 years ago.
Here is what I have:
script_1.sh
echo "HELLO FROM script_1.sh"
./scripts/script_2.sh
echo $MY_VARIABLE
script_2.sh
echo "HELLO FROM script_2.sh"
export MY_VARIABLE="MY_VALUE"
Here is what it logs when I run script_1.sh:
HELLO FROM script_1.sh
HELLO FROM script_2.sh
"" // EMPTY LINE INSTEAD OF "MY_VALUE"
What am I doing wrong?
Variables are exported from a parent to a child, not vice versa. script_2.sh is called in a different shell whose environment doesn't propagate back to the parent shell.
Source the script (using the .) to call it in the same shell. You then don't even need to export the value.
. ./scripts/script_2.sh
environment variables are inherited down the call chain. they are not returned up to the caller.
in other words: a called script might inherit the variables of the caller. but the caller will not get the variables of the called script.
in you simple example the easiest solution is to "source the script"
. ./scripts/script_2.sh
(the dot is the command to source a script)
sourcing is not a new step in the call chain. instead the sourcer and sourcee share the same environment. for more explanation on the difference of executing and sourcing see here: https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-vs-sourcing-it/176788#176788
there are other options but they are more complicated and error prone. it seems that you are starting to learn shell scripting. so learn the difference of sourcing and executing and the implication on the environment for now. if you need other options later then come back and ask another question.

Calling and passing variables values between shell scripts [duplicate]

This question already has answers here:
Pass all variables from one shell script to another?
(7 answers)
Closed 3 years ago.
OS: Ubuntu 18.04
Bash
I am trying to get one shell script to pass variables to another script and execute it. Here's what I tried:
mainscript.sh
#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")
subscript1.sh:
#!/bin/bash
# subscript1.sh
echo $test_string
But, when I do:
bash mainscript.sh
I get nothing. Any ideas on how to do this?
Shell variables are by default not visible in child processes. To pass them to children use the export keyword:
#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
export test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")

Using a environment variable defined in .bashrc from a bash script [duplicate]

This question already has answers here:
What does "export" do in shell programming? [duplicate]
(3 answers)
Closed 2 years ago.
If we set custom environment variables in .bashrc like the following:
TMP_STRING='tmp string'
It seems like this variable is not directly accessible from the bash script.
#!/bin/bash
echo $TMP_STRING
I tried the following, but it also doesn't work:
#!/bin/bash
source ~/.bashrc
echo $TMP_STRING
Could you suggest what would be the correct way in this case?
Thank you!
Just VAR=value defines a shell variable. Environment variables live in a separate area of process memory that is preserved when another process is started, but are otherwise indistinguishable from shell variables.
To promote a variable to an environment variable, you must export it.
Example:
VAR=value
export VAR
or
export VAR=value
If you put the above into .bashrc, the above value of $VAR should be available in the script, but only if it's run from the login shell.
I would not recommend sourcing .bashrc in the script. Instead, create a separate file named something like .script.init.sh and source that:
# script init
TMP_STRING='tmp string'
Your script:
# script
. ~/.script.init.sh
If this value must be available to any process spawned by the script, prefix it with export :
# script init
export TMP_STRING='tmp string'

bash export not setting variable [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 a simple environment setup script that exports some environment variables like so.
#!/bin/sh
export NEWROOT=~/some/directory
echo $NEWROOT
This echos the correct directory, but after its run, when I echo $NEWROOT in the same shell, it returns nothing.
Any idea why the variable isn't setting?
The shell is run in a separate process, and environment variables in a child process do not affect the environment variables in the parent process.
If you want to run the script in the same process, you can use the dot command, like this:
. myscript
A child process can't affect the environment variables of its parent. If you source the script instead, that will evaluate the script in the current environment, leaving NEWROOT.

Set variable from script [duplicate]

This question already has an answer here:
Best way to set environment variables in calling shell
(1 answer)
Closed 8 years ago.
I have a script "set_var.sh" written like this
#!/bin/bash
export NAME=release
export ROOT=/Volumes/name/dev/release
but if I run this set_var.sh from terminal, afterward I issue set command to check variables I could not find NAME and ROOT var be set.
I am wondering what is wrong in my case.
it was set in sub-shell.
you need
source set_var.sh
If you simply run set_var.sh, it runs in its own shell which exits, losing the variables that were set.
If you want to change variables in your interactive shell, you can use:
source set_var.sh
or the shorthand,
. set_var.sh
This will execute the lines of the script as if they were typed into your interactive shell.
Note that when you "source" a file this way, it does not require the "shebang" on the first line.
Note also that this is feature exists in Bourne shell as well, but only in the short-form version.

Resources