How to export environment variable from a bash script
Linux - NewbieThis 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.
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.
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?
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.
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.
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.
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.
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)
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.
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.
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...
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
. (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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.