LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


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

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


Hello.

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).

Thanks
 
Old 06-24-2010, 10:31 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Code:
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.

Code:
myscripts ()
{
    pathadd="$HOME/my_scripts";
    case $1 in
        on)
            [[ $PATH =~ $pathadd ]] || export PATH="${PATH}:$pathadd"
        ;;
        off)
            export PATH="${PATH//:$pathadd/}"
        ;;
    esac
}

Last edited by David the H.; 06-24-2010 at 10:37 AM.
 
1 members found this post helpful.
Old 06-24-2010, 01:03 PM   #3
MichaelG67
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 05:06 AM.
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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