LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2010, 05:36 AM   #1
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Rep: Reputation: 15
BASH: Global Variables


Hi all,

I have a problem with a very big script I wrote in bash, and now I need to modulirize it in at least four smaller scripts. The problem is, that most of the variables I have will need to be shared by all scripts.

My question is: is there a way to declare global variables in bash? So that I can use and change them in any of the scripts and every change in the variable can be "seen" by the other scripts later.
 
Old 01-23-2010, 06:09 AM   #2
konzo
Member
 
Registered: Dec 2008
Location: Bulgaria
Distribution: Debian / Slackware / Ubuntu
Posts: 230

Rep: Reputation: 34
Did you try with export? Example:

export VAR="set"

or

export VAR="set" script.sh or export VAR="set" sh script.sh in command line.

Be sure that the script is executable.

Last edited by konzo; 01-23-2010 at 06:11 AM.
 
Old 01-23-2010, 06:16 AM   #3
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Hi konzo,

yes I tried the "export" command and it works once I'm passing the value of the variable from script1 to script2, then a change it in script2 to a new value. But once script2 ends and script1 is activated again, the new value from the variable is not used further. Script1 still has the old value, passed before to script2. Is there a solution for that?
 
Old 01-23-2010, 06:51 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
If you use the built-in source command to invoke the child scripts from the main script, then the variables will be shared (you won't even need to export them to the child scripts).
 
Old 01-23-2010, 07:55 AM   #5
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Hi neonsignal,

the solution with "source" works perfectly! Many thanks
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
global variables Swapna173 Linux - Newbie 2 03-19-2009 01:01 AM
Bash: How to setup "global" environment variables? ArthurHuang Programming 2 02-07-2008 12:24 PM
Global Variables In PHP? windisch Programming 2 06-12-2006 12:51 PM
exporting global variables neo77777 Programming 6 09-18-2002 01:46 PM
global environmental variables antadam Linux - General 2 10-11-2001 06:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:40 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