Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 06-24-2010, 10:37 AM   #1
LQ Newbie
Registered: Jun 2010
Posts: 3

Rep: Reputation: 0
In BASH shell, how do I export a variable from a script.


I have read several threads on this already, but none of the solutions work for me or apply to my situation.

I need to be able to set a variable that is used by my build environment. I need to be able to change this variable "on the fly" as I work with multiple build environments. A portion of the variable is arbitrary, so I need to treat the arbitrary portion of the variable as an argument.

I would normally just create an alias, but BASH doesn't support arguments to aliases. So the workaround for the no-alias-arguments bug is to use a script. No problem. Except the variable I set in the script does not exist when I exit the script.

Now, if I run the script by using "$ . myscript" it works in that the variable is set after it exits. The problem is the argument checking I have in the script doesn't work anymore.

BASH aliases don't support arguments and I can't export a variable from a script unless I source the script (is it even a script at that point?) Maybe I am simply taking the wrong approach. So I am open to other approaches to solving the original problem (should of changing my build environment of course).

Old 06-24-2010, 11:31 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
There is no way to directly set a variable in a shell from a script. Commands can only affect the current shell or subshells below it. Your only options are to set it in the shell itself or source it from a file.

And no, you can't arguments in aliases, but you can use them in shell functions. So one thing you can do is create a function like this, place it in your bashrc or other sourced file, and you'll have the ability to set the variable on the fly.

function setvar {
	export MYVAR="$1"
Edit: by the way, I personally use a similar function to "turn on" and off a set of scripts that I have by modifying the PATH variable on the fly. It works like a charm.

myscripts ()
    case $1 in
            [[ $PATH =~ $pathadd ]] || export PATH="${PATH}:$pathadd"
            export PATH="${PATH//:$pathadd/}"

Last edited by David the H.; 06-24-2010 at 11:37 AM.
1 members found this post helpful.
Old 06-24-2010, 02:03 PM   #3
LQ Newbie
Registered: Jun 2010
Posts: 3

Original Poster
Rep: Reputation: 0
That was it. I had the wrong approach. A function was the way to go. Thank you David_the_H.

Last edited by MichaelG67; 06-25-2010 at 06:06 AM.


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 08:12 AM
Bash/Shell: Problem using a variable in a script redvivi Linux - General 8 08-21-2008 03:02 PM
how to export a bash shell variable before init.d process start pnsidhu Linux - Newbie 1 06-04-2008 09:21 AM
Bash shell script - variable magic needed (no idea how to approach this) arrenlex Programming 8 03-02-2006 06:41 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 02:54 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration