Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 99
  • Last Modified:

Export shell variable

Hi ,

In my script i use export variable . So i can use the variable in another script which was sourced from main script.

But in command line, if i use $variable i see an empty value returned.

Please help on this. My aim is if some problem happened the script will be started in middle so if they start script 2 it should aware of the variable which was set in main script.

Thanks
0
magento
Asked:
magento
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
ozoCommented:
You would need to source the script that defined $variable from the command line in order to use $variable in the command line.
0
 
woolmilkporcCommented:
"export" makes a variable available to subshells of the shell where it was issued, but never to the parent of that shell.

That's a security measure to avoid that a script could modify the environment of your login shell.
As ozo said, you must source the script which exports the variable to make it available to the current shell (= the parent of the script containing "export"):

. scriptname # note it's  "dot space" on the left!)

or (only in bash)

source scriptname
0
 
magentoAuthor Commented:
say script1.sh has a variable named code_version . This is the input to script2.sh

Sometimes the system will fail for unknown causes ..at that time, script 1.sh completed so we will start script2.sh . At that time the variable input will not be available.

Can you advice me how to solve this issue ?

Script1.sh
export $(code_version=1.7)

script2.sh
if [ $code_version == 1.7 ] then ...

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
woolmilkporcCommented:
. Script1.sh

Please note that the script must not contain "exit"! This would terminate the calling shell (probably your login shell)!

By the way, what's this strange syntax "$(code_version=1.7)" ??
This will definitely not work (at least not in the shells I use to use).

Correct syntax is simply:

export code_version="1.7"
0
 
simon3270Commented:
If you want variables to be seen in other scripts, your best bet is to write the valeus to a file, as:

script1.sh
  code_version=1.7
  echo $code_version > /tmp/code_ver.txt

script2.sh
   code_ver=$(cat /tmp/code_ver.txt)
   echo Code version is $code_ver

Note that the variable name does not have to be the same in both scripts.


An alternative is to write the definition of the variable to the file, as

script1.sh
  echo export code_version=$code_version > /tmp/code_ver.txt

then source that file in the second script to define the value

script2.sh
  . /tmp/code_ver.txt

In this case, the variable name in script2.sh will be whatever you wrote to /tmp/code_ver.txt.
0
 
ozoCommented:
If you want the input to script2.sh to come from script1.sh,
you can pipe the input;
script1.sh | script2.sh
with
cat > script1.sh <<< 'echo 1.7'
cat > script2.sh <<< 'read code_ver; echo $code_ver'
or pass an arg:
script2.sh `script1.sh`
with
cat > script2.sh <<< 'echo $1'
or call script2 from script1
with
cat > script1.sh <<< 'script2.sh 1.7'
or
cat > script1.sh <<< 'echo 1.7 | script2.sh
or
cat > script1.sh <<< 'export code_ver=1.7; script2.sh'
or call script1 from script2
with
cat > script2.sh <<< 'code_ver=`script1.sh`; echo $code_ver'
or with
cat > script1.sh <<< 'code_ver=1.7'
cat > script2.sh <<< '. script1.sh; echo $code_ver'
or with
cat > script1.sh <<< 'exit 17'
cat > script2.sh <<< 'script1.sh; echo $?'
0
 
simon3270Commented:
Just remember that sourcing a shell script updates your current shell environment, possibly affecting anything you do afterwards (e.g. adding entries to the PATH, changing current directory, creating function names that silently overlay commands and so on).

It will probbaly be fine for you, but is something to bear in mind.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now