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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
07-17-2005, 05:16 PM
|
#1
|
LQ Newbie
Registered: Jul 2005
Posts: 6
Rep:
|
How to export environment variable from a bash script
Hi everyone!
I am new to bash scripting. I am trying to write a simple bash script to automate some command typping. Let's say that i can type on command prompt: "export LFS=/mnt/lfs". Then when i type on that same prompt "echo $LFS", i have the output "/mnt/lfs". Now what i dont know how to do (and i tried to read up on this and tried by trial and error, but without success.) is how to export that same variable $LFS but from a bash script. In other words, when my bash script finishes and when i type "echo $LFS" i want to have the same result: "/mnt/lfs". Can anyone enlighten me on this?
Thanks. Appriciate in advance.
Last edited by dimaash; 07-17-2005 at 05:17 PM.
|
|
|
07-17-2005, 05:39 PM
|
#2
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Re: How to export environment variable from a bash script
Quote:
Originally posted by dimaash
Hi everyone!
I am new to bash scripting. I am trying to write a simple bash script to automate some command typping. Let's say that i can type on command prompt: "export LFS=/mnt/lfs". Then when i type on that same prompt "echo $LFS", i have the output "/mnt/lfs". Now what i dont know how to do (and i tried to read up on this and tried by trial and error, but without success.) is how to export that same variable $LFS but from a bash script. In other words, when my bash script finishes and when i type "echo $LFS" i want to have the same result: "/mnt/lfs". Can anyone enlighten me on this?
Thanks. Appriciate in advance.
|
Variables can only be exported to subordinate processes, you can't pass them back up to the parent. If you really want your script to affect the parent shell's environment, run the script as
Hint: don't put an exit statement in your script, or your shell will terminate.
|
|
1 members found this post helpful.
|
07-17-2005, 05:47 PM
|
#3
|
Member
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944
Rep:
|
What exactly are you trying to accomplish with the script? You can always put export statements in your .bashrc file if you want them to affect everything.
|
|
|
07-17-2005, 06:53 PM
|
#4
|
LQ Newbie
Registered: Jul 2005
Posts: 6
Original Poster
Rep:
|
jrdioko:
I am trying to automate the building process of Linux from Scratch (I know that there is an ALFS out there, but i want to do something rather small and my way, and at the same time grab the opportunity to learn bash). As you know, it is a long process and as you go further and further in it, there are more and more commands to type. So, if let's say somewhere near the middle of that build process i accidently destroy toolchain or temporary environment i would probably restart all over again since i don't have the knowledge to fix the problem. So, i thought it would be nice to put all those commands in a bash script and let it run up to the point where i screwed up.
|
|
|
07-17-2005, 07:14 PM
|
#5
|
Member
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944
Rep:
|
It seems like you could put global export statements in .bashrc and then have the script do the rest, but that's probably not proper shell scripting design. I'll let someone else jump in.
|
|
|
07-31-2009, 09:35 AM
|
#6
|
Member
Registered: Mar 2009
Distribution: Slackware
Posts: 121
Rep:
|
Quote:
Originally Posted by eddiebaby1023
Variables can only be exported to subordinate processes, you can't pass them back up to the parent. If you really want your script to affect the parent shell's environment, run the script as
Hint: don't put an exit statement in your script, or your shell will terminate.
|
I'm also trying to pass some variables to a parent script. Any other ways to do it?
Your method seems to work. I don't know why, though. Can you direct me to some documentation on what that leading dot is and why it works? (i'm not going to include it without understanding what it does, or it will backfire; it always does)
|
|
|
07-31-2009, 09:42 AM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by chicken76
Your method seems to work. I don't know why, though. Can you direct me to some documentation on what that leading dot is and why it works? (i'm not going to include it without understanding what it does, or it will backfire; it always does)
|
From man source or man bash_builtins
Code:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit
status of the last command executed from filename.
plus http://www.tldp.org/LDP/abs/html/abs...html#SOURCEREF
|
|
|
08-14-2010, 09:20 AM
|
#8
|
LQ Newbie
Registered: Jul 2010
Posts: 2
Rep:
|
Hi...
You can export and add new variables to current shell with function, because function is executing in current shell...Functions rule!
|
|
0 members found this post helpful.
|
08-24-2010, 01:22 AM
|
#9
|
LQ Newbie
Registered: Nov 2009
Posts: 25
Rep:
|
bit confused
Quote:
Originally Posted by DElimitER
Hi...
You can export and add new variables to current shell with function, because function is executing in current shell...Functions rule!
|
...not sure i get it
how about a simple working example... thanks
|
|
0 members found this post helpful.
|
08-24-2010, 01:40 AM
|
#10
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Goodness ... a corpse dug up twice!
|
|
|
08-24-2010, 01:47 AM
|
#11
|
LQ Newbie
Registered: Nov 2009
Posts: 25
Rep:
|
....
Quote:
Originally Posted by Tinkster
Goodness ... a corpse dug up twice!
|
look
i m not smart, ok!
|
|
|
08-24-2010, 01:58 AM
|
#12
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by kusanagiyang
look
i m not smart, ok!
|
Not sure what that was a reference to ... I'm referring
to the fact that this thread is from 2005; it died, got
re-animated in August 2009, died, and got dug up again in
August this year.
Cheers,
Tink
|
|
1 members found this post helpful.
|
08-24-2010, 02:03 AM
|
#13
|
Member
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944
Rep:
|
Quote:
Originally Posted by Tinkster
Not sure what that was a reference to ... I'm referring
to the fact that this thread is from 2005; it died, got
re-animated in August 2009, died, and got dug up again in
August this year.
Cheers,
Tink
|
Crazy. Also crazy Tinkster is still around, I think I remember him from 2005...
|
|
|
08-24-2010, 02:09 AM
|
#14
|
LQ Newbie
Registered: Nov 2009
Posts: 25
Rep:
|
thanks, it works
Quote:
Originally Posted by eddiebaby1023
Variables can only be exported to subordinate processes, you can't pass them back up to the parent. If you really want your script to affect the parent shell's environment, run the script as
Hint: don't put an exit statement in your script, or your shell will terminate.
|
really appreciated it.
i just dont know why it works
|
|
|
08-24-2010, 05:17 AM
|
#15
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
. (or source in bash) reads the script to the current process or environment while just running ./script.sh will spawn a new process based from the type of executable referred to the header of the script. For example will call bash.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 11:25 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|