LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2013, 04:52 PM   #1
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
bash: export variable in current or parent shell


Hi. I'm making bash scripts that are supposed to set environment variables for certain software modules. (E.g., I would run a libXYZ environment script before trying to compile anything with libXYZ.) However, it seems that if I run a bash script (outside of .bashrc or .bash_profile) that the variables are only set for the script itself and children of the script. How do I either 1) get the script commands to run inside the parent shell (instead of a subshell) or 2) get the variables to export up to the parent shell?

If I'm not making sense let me know and I will try to explain more.
 
Old 09-14-2013, 05:12 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

To make variables visible in parent shell use source <scriptname> (or . <scriptname>) instead of executing it via ./<scriptname>.
 
1 members found this post helpful.
Old 09-14-2013, 10:46 PM   #3
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467

Original Poster
Blog Entries: 60

Rep: Reputation: 51
Thanks firstfire!

I wanted to mention, for posterity's sake: when using source, all the variable setting commands will affect your shell, not just the export commands. This can cause problems if you weren't expecting it. In short, you can't have "local" variables in top-level of the script you source.

A way around this is to wrap your entire script in a function call, use the local builtin, and call the function. E.g.,

Code:
# script to be sourced

function main()
{
  local foodir=foo # scope limited to function
  export PATH=${HOME}/${foodir} # global scope
}

main
If there is a better way to do that, I'd be glad to know.
 
  


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
export variable to parent shell barunparichha Linux - Software 3 02-01-2011 03:20 AM
In BASH shell, how do I export a variable from a script. MichaelG67 Linux - Software 2 06-24-2010 01:03 PM
how to export a bash shell variable before init.d process start pnsidhu Linux - Newbie 1 06-04-2008 08:21 AM
variable export in shell MarioT Programming 3 05-16-2006 11:52 PM
BASH variable export Barbarian Programming 2 11-27-2001 08:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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