Solved

Export shell variable

Posted on 2015-02-10
7
93 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
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell code to list items in dropdown 6 89
How to enable sync between two yum repo? 1 39
Run Secure WMI query from CentOS 5 82
AWK: Pythagoras bp script 18 27
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.

856 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