Why isn't exporting a variable working in the following case:
In the following example I export the PARAM variable and set the sleep to 1000 second in order to run the script as process on the background.
#!/bin/bash
export PARAM="I AM A REAL VALUE"
sleep 1000
so I execute the script as process as the following:
/tmp/example.bash &
Now script runs as a process (I checked it with ps -ef) and from the Linux console I want to print the $PARAM as the following
echo $PARAM
but no value from PARAM variable.
Why? The export from the script isn’t exporting the value when the script process is running.
When you run /tmp/example.bash &, you set the environment in the sub-shell, but that does not affect the parent shell that ran it.
You need to (a) remove the sleep 1000 and (b) use the . command or (in Bash or a C shell) the source command to read the file as part of the current process:
sed -i.bak '/sleep/d' /tmp/example.bash # GNU or BSD sed
. /tmp/example.bash
echo $PARAM
Related
so basically i have a shell script below and i wanted to know what the value of x is after the code has been executed. What does the export does in shell scripting?
export x=4
bash
echo $x
x=100
echo $x
exit
echo $x
In theory echo will say [in order]:
4
100
then the script exits, but from the terminal you could enter the last echo manually and it would say
4
"export", from what i'm gathering is like the windows environmental variables...
user#machine~$export
declare -x .....
So everything that's listed from the base command in the terminal are things like $USER $LANG $HOME $SHELL, which can all be used to insert global variables into your script. For example,
#!/bin/bash
mkdir /$HOME/newdir
touch /$HOME/newdir/newfile
The commands you entered would have different outputs reliant on whether or not you typed them into your terminal or ran from script. If you ran from script x would equal the last thing you assigned it to via "export" or locally assigning a value to it via "x=3".
The script will hold the last assigned value until exit. "Export" will save the variable forever.
Is it possible to export a variable from a parent shell script to its children ?
Trying the execute the two following scripts it always returns to me 0 but I want it to return 3. I`ve also tried to export, set and add the variable error to the .bash_profile without success...
test.sh
$ cat test.sh
#!/bin/bash
error=0
./envtest.sh
echo $error
envtest.sh
$ cat envtest.sh
#!/bin/bash
source ./test.sh
test=3
error=$test
echo $error
Like #chepner commented, no.
When you invoke ./envtest.sh from within test.sh, a new process is created to run /bin/bash ./envtest.sh, and that process's environment is initialized with a copy of every environment variable from the parent process. No matter what you do inside envtest.sh, it can only impact the variables within its own environment; it cannot touch the variables in the parent's environment.
Raspbian (Jessy) - root#Raspberry Pi - Putty
In the Terminal i type in
finalanswer=0
now i got a script with this code
#!/bin/bash
source /lib/lsb/init-functions
echo $finalanswer #just as a test
if [ ! "$finalanswer" = "0" ]
then
rm -r mnt/objects/all
log_warning_msg "All Files has been deleted" || true
touch its_over.txt
else
let finalanswer=1
log_action_msg "Var finalanswer was 0. setting back to 1" || true
fi
there is a cronjob that starts this script every hour
sooo. somewhere there must be an error.
because he is reading the Variable $finalanswer as nothing.
that means variables that has been defined outside of this script will not work?
how do i fix this?
Shell variables are not inherited by child processes. If you want a variable to be inherited, it has to be an environment variable. You create environment variables using the export command.
export finalanswer=0
or
finalanswer=0
export finalanswer
You can also export a variable just for the duration of a command by putting the assignment at the beginning of the command:
finalanswer=0 /path/to/script
Note that variables you assign in your shell will not be accessible to cron jobs. Variables can only be exported to processes that are descended from the shell, and processes run by cron are not related to your shell process. If you want to set a variable for use in a cron job, you can put the assignment into the crontab file itself.
You can define a variable for a single command by placing its definition before the command you wish to run:
$ VARIABLE=hunter perl -E 'say $ENV{VARIABLE}'
hunter
you can do the same thing for a cron entry:
*/10 * * * * VARIABLE=hunter <command>
I have two shell scripts test1.sh and test2.sh.
In test1.sh I have the below statements :
In, test1.sh,I have a variable I whose value will be used by test2.sh
#!/bin/sh
I="10"
echo $I
In, test2.sh, the same value of the variable will be copied and printed
#!/bin/sh
J=$I
echo $J
I need to run both the scripts in crontab, I tried export command, but nothing worked.
Add this to you crontab :
. ./test1.sh && ./test2.sh;
And modify you test1.sh like that :
#!/bin/sh
export I="10"
echo $I
With . the first will be executed as source and will hold variables.
Both scripts are running in their own shell, and only share their environment with their parent process. If you want two separate shell scripts to share environment variables, the variables have to be set (and exported) in the parent process before calling the scripts.
You could also create a third script that only sets the variables, and source that script from the two main scripts.
If you want to use the output of test1.sh in script test2.sh, you have two options
store the output of test1.sh in a file and read that file back in test2.sh
call test1.sh from test2.sh
test2.sh:
#!/bin/sh
J=$(test1.sh)
echo $J
As #Joachim Pileborg already suggested, you can set (but not echo) variables in one script and source it in the other one
test1.sh
I="10"
J=20
K=30
test2.sh
source test1.sh
# do something with I, J, K
I have two scripts 1.sh and 2.sh.
1.sh is as follows:
#!/bin/sh
variable="thisisit"
export variable
2.sh is as follows:
#!/bin/sh
echo $variable
According to what I read, doing like this (export) can access the variables in one shell script from another. But this is not working in my scripts.
If you are executing your files like sh 1.sh or ./1.sh Then you are executing it in a sub-shell.
If you want the changes to be made in your current shell, you could do:
. 1.sh
# OR
source 1.sh
Please consider going through the reference-documentation.
"When a script is run using source [or .] it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script."
export puts a variable in the executing shell's environment so it is passed to processes executed by the script, but not to the process calling the script or any other processes. Try executing
#!/bin/sh
FOO=bar
env | grep '^FOO='
and
#!/bin/sh
FOO=bar
export FOO
env | grep '^FOO='
to see the effect of export.
To get the variable from 1.sh to 2.sh, either call 2.sh from 1.sh, or import 1.sh in 2.sh:
#!/bin/sh
. ./1.sh
echo $variable