LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2006, 06:24 AM   #1
MarioT
Member
 
Registered: May 2004
Posts: 52

Rep: Reputation: 15
variable export in shell


If I open a shell, then it's subshell. In subshell I define variable. How can I export this variable to bi visible in parent shell.
export command export variable only to subshells but not to parent shell.
Define it as system variable doesn't work for me.

Thanks.

Mario
 
Old 05-15-2006, 06:38 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Write the value to a file, and have the parent read the file and export the value.

Have the child kill off the parent.

-- There is a very good reason why this works the way it does. You could not have
two children doing different jobs, then exiting. What the parent would then have in environment variables would be unpredictable. Unpredictable=chaos in computing.
 
Old 05-15-2006, 07:37 AM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,508
Blog Entries: 2

Rep: Reputation: 68
In scripts, you can source the sub-shell. All variables defined in the sub-shell will be visible in the parent.

/tmp/parent.sh
Code:
#!/bin/bash


. /tmp/subshell.sh
echo $MYVAR


/tmp/subshell.sh
Code:
#!/bin/bash

echo $MYVAR
MYVAR=othervalue
execution output
Code:
bash-3.1$ /tmp/parent.sh

othervalue
bash-3.1$
 
Old 05-16-2006, 11:52 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430

Rep: Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787
Note that if you source the shell script, it becomes part of the parent, so no sub-shell is involved(!)
 
  


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
How to export environment variable from a bash script dimaash Linux - Newbie 20 08-09-2011 07:12 AM
export cd variable? rjwhite Linux - Games 1 12-17-2005 05:47 PM
Export variable loopoo Linux - Newbie 4 08-08-2005 04:08 AM
Export Shell Variable not working maxhugen Linux - Newbie 6 01-06-2005 10:12 PM
BASH variable export Barbarian Programming 2 11-27-2001 08:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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