LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-17-2005, 04:16 PM   #1
dimaash
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Rep: Reputation: 0
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 04:17 PM.
 
Old 07-17-2005, 04:39 PM   #2
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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
Code:
.  ./yourscript
Hint: don't put an exit statement in your script, or your shell will terminate.
 
1 members found this post helpful.
Old 07-17-2005, 04:47 PM   #3
jrdioko
Member
 
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Rep: Reputation: 30
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.
 
Old 07-17-2005, 05:53 PM   #4
dimaash
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 07-17-2005, 06:14 PM   #5
jrdioko
Member
 
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Rep: Reputation: 30
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.
 
Old 07-31-2009, 08:35 AM   #6
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Rep: Reputation: 1
Quote:
Originally Posted by eddiebaby1023 View Post
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
Code:
.  ./yourscript
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)
 
Old 07-31-2009, 08:42 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by chicken76 View Post
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
 
Old 08-14-2010, 08:20 AM   #8
DElimitER
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
Talking

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.
Old 08-24-2010, 12:22 AM   #9
kusanagiyang
LQ Newbie
 
Registered: Nov 2009
Posts: 21

Rep: Reputation: 1
bit confused

Quote:
Originally Posted by DElimitER View Post
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.
Old 08-24-2010, 12:40 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Goodness ... a corpse dug up twice!
 
Old 08-24-2010, 12:47 AM   #11
kusanagiyang
LQ Newbie
 
Registered: Nov 2009
Posts: 21

Rep: Reputation: 1
....

Quote:
Originally Posted by Tinkster View Post
Goodness ... a corpse dug up twice!
look
i m not smart, ok!
 
Old 08-24-2010, 12:58 AM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by kusanagiyang View Post
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.
Old 08-24-2010, 01:03 AM   #13
jrdioko
Member
 
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Rep: Reputation: 30
Quote:
Originally Posted by Tinkster View Post
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...
 
Old 08-24-2010, 01:09 AM   #14
kusanagiyang
LQ Newbie
 
Registered: Nov 2009
Posts: 21

Rep: Reputation: 1
thanks, it works

Quote:
Originally Posted by eddiebaby1023 View Post
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
Code:
.  ./yourscript
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
 
Old 08-24-2010, 04:17 AM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
. (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
Code:
#!/bin/bash
will call bash.
 
1 members found this post helpful.
  


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 unset environment variable in bash suneel Linux - Newbie 7 09-14-2009 10:17 AM
export call in bash script Donald1000 Linux - Software 10 03-12-2009 08:03 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
set and export using bash script acummings Linux - General 10 01-03-2005 02:22 PM
BASH variable export Barbarian Programming 2 11-27-2001 08:37 PM


All times are GMT -5. The time now is 07:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration