Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-23-2010, 06:36 AM   #1
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, 07:09 AM   #2
Registered: Dec 2008
Location: Bulgaria
Distribution: Debian / Slackware / Ubuntu
Posts: 230

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

export VAR="set"


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

Be sure that the script is executable.

Last edited by konzo; 01-23-2010 at 07:11 AM.
Old 01-23-2010, 07:16 AM   #3
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, 07:51 AM   #4
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, 08:55 AM   #5
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Hi neonsignal,

the solution with "source" works perfectly! Many thanks


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:01 AM
Bash: How to setup "global" environment variables? ArthurHuang Programming 2 02-07-2008 01:24 PM
Global Variables In PHP? windisch Programming 2 06-12-2006 01:51 PM
exporting global variables neo77777 Programming 6 09-18-2002 02:46 PM
global environmental variables antadam Linux - General 2 10-11-2001 07:36 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:28 AM.

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