LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-20-2014, 08:36 AM   #1
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Rep: Reputation: 5
Question BASH: Variable Passing Between Two Scripts - and Back Again


I'm not sure I can do this.

I have script-A execute a bunch of stuff. To make it shorter I broke some of out of of the "stuff" to another script, called script-B.

In script-B I collect info from user and I export the variables I create.

Script-A is not picking up those exported variables as Script-A continues to do perform more "stuff" after script-B finishes. Using bash's "set -u" option show's that the variables are unset in the eyes of script-A.

How do I pass script-B's variables back to script-A if the export VARIABLE entries do not pass back the values? I do source other values in a common file, so all that works fine.

I really don't want to write the values into files only to have script-A suck them back in, that seems lame, Yes?

So, looks something like this.

script-A ---> executes script-B ---> script-A continues looking for B's variables.

Thanks!
 
Old 12-20-2014, 08:50 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,344

Rep: Reputation: Disabled
When you call script B from script A, script B runs in a separate instance of the shell. This instance receives a copy of the current environment, hence all variables from script A will be available to script B. However, once script B ends, control is returned to the shell instance running script A, and any changes script B may have made to its environment is lost.

Solution: Rather than calling script B from script A, use the "source" command/function:
Code:
[... this is script A ...]

. ./path/to/scriptb

[... rest of script A ...]
Note the dot before the path; that's the "source" command. It cause the entire contents of script B to be "imported" into script A, and thus it will run in the same shell instance as script A.
 
Old 12-20-2014, 11:01 AM   #3
bits45
Member
 
Registered: Jul 2009
Posts: 44

Original Poster
Blog Entries: 2

Rep: Reputation: 5
Thumbs up

Well heck, that must of been a really stupid question to ask.

Naturally that worked great. I guess I couldn't see the forest thru the trees.

Thanks for the quick reply.

Worked great.

take care
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] passing variable through script (bash) ted_chou12 Programming 1 11-20-2011 10:17 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Bash - Passing variable to ssh zepphead5 Programming 8 05-09-2010 09:34 AM
Passing a variable to bc in bash - HELP! rodhull Programming 4 01-14-2009 06:59 PM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration