LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-06-2009, 01:42 PM   #1
deepakc
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Rep: Reputation: 0
Scope of shell script variables ?


I have a shell script file "test.sh", which does this
export TEST_VARIABLE=linux

I call this from a terminal
and after it gets executed when i try to
get the value of the variable TEST_VARIABLE in the same terminal, its empty. What's the scope of this variable ?

$ ./test.sh
$ echo $TEST_VARIABLE
$


I need variables that i export in a sh file (and call it from a terminal) to live until the terminal exists. How can i get it ?

Thnks.,

Last edited by deepakc; 07-06-2009 at 01:43 PM.
 
Old 07-06-2009, 02:19 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
you need to source the script:
Code:
source test.sh
 
Old 07-06-2009, 02:53 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Learn something new every day!!!

I had thought that your example would work, but it will not. Here is the relevant quote from "ABS"*:
Quote:
A script can export variables only to child processes, that is, only to commands or
processes which that particular script initiates. A script invoked from the
command-line cannot export variables back to the command-line environment.
Child processes cannot export variables back to the parent processes that spawned
them.
*ABS = The Advanced BASH Scripting Guide---the favorite boat anchor for scripters with OCD**. Get it (ABS, not OCD) at http://tldp.org

**OCD = Obsessive-Compulsive Disorder
 
Old 07-06-2009, 06:11 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,347

Rep: Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365
You may also see the (older) syntax

. script.sh

That's a 'dot' then a 'space', instead of 'source<space>'
 
Old 07-07-2009, 12:29 AM   #5
deepakc
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Hi.,

both

source script.sh
. script.sh

works...

Thnks.,
 
  


Reply


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
importing variables into shell script? ocicat Programming 5 08-12-2008 03:15 AM
Shell script variables Fredde87 Linux - Newbie 13 11-01-2006 03:35 AM
shell script variables Gary_Menegon Programming 1 10-02-2006 09:28 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
Why?? can not use variables with shell script Bassam Linux - General 9 01-27-2004 07:42 AM


All times are GMT -5. The time now is 05:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration