Help answer threads with 0 replies.
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 04-22-2011, 06:52 AM   #1
Registered: Aug 2008
Posts: 553

Rep: Reputation: 33
Question [bash] Exported variable gone after end of script?


I'm using a simple Bash script to export an environment variable, eg.

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/my/own/dir
echo $PATH
Then, when I need to add /my/own/dir to PATH, I simply run ./update.path.bash.

Problem is, this change only occurs within the context of the script, and doesn't live beyond it.

What is the right way to update an environment variable so that it "sticks"?

Thank you.
Old 04-22-2011, 07:05 AM   #2
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,338

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362

When you start a script, it runs in its own process. A process can't set the environment variables of the parent. But you can run a script without starting a new process. To do this, you have to prefix it with "source" or simply ".".

source ./update.path.bash


. ./update.path.bash
Old 04-26-2011, 03:33 AM   #3
Registered: Aug 2008
Posts: 553

Original Poster
Rep: Reputation: 33
Thanks for the tip.


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[SOLVED] calling bash script with more than one variable from python script mfb Programming 4 10-12-2010 07:51 AM
bash script: use the directory of the script file as variable? phling Linux - Newbie 12 01-16-2010 08:16 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 04:08 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 03:40 PM > Forums > Linux Forums > Linux - Software

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