Solved

Export shell variable

Posted on 2015-02-10
7
96 Views
Last Modified: 2015-02-17
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
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 40601136
You would need to source the script that defined $variable from the command line in order to use $variable in the command line.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 200 total points
ID: 40601152
"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
 
LVL 5

Author Comment

by:magento
ID: 40601167
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40601187
. 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
 
LVL 19

Expert Comment

by:simon3270
ID: 40601316
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
 
LVL 84

Expert Comment

by:ozo
ID: 40601420
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
 
LVL 19

Expert Comment

by:simon3270
ID: 40614528
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question