LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-20-2010, 03:46 AM   #1
skiron.liu
Member
 
Registered: Jun 2009
Location: Harbin
Posts: 32

Rep: Reputation: 0
Why I can't set environment value in my BASH script?


for example
Code:
#!/bin/bash
  PATH=$PATH:/opt/bin
  export PATH
I run "echo $PATH" after this script. The "/opt/bin" don't append to "PATH".
 
Old 06-20-2010, 04:01 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The reason is that every script/process has its own environment (scope) and using the export statement it can transfer variables or functions to its child processes, but not backward to the parent shell. You can achieve this task by "sourcing" the script, not by "executing" it. In bash this means:
Code:
. test.sh
where the dot is the source command. In this way all the statements inside the script are simply executed in the current shell and all the variable assignments are in effect.

This is the reason why environment variables are defined in configuration files like /etc/bashrc, /etc/profile or $HOME/.bashrc and $HOME/.bash_profile. These and/or other files are sourced (not executed) every time you start a new shell session. Hope this helps.
 
1 members found this post helpful.
Old 06-20-2010, 04:01 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

If you execute a script, the variables set are only valid/accessible in that, newly created, shell and it's sub-shells and not in the shell you executed the script in (mother - child principle).

If you want those variables to be accessible in your current shell you need to parse the script, not execute it:
source <scriptname> or . <scriptname>

Hope this helps.
 
1 members found this post helpful.
Old 06-20-2010, 04:25 AM   #4
skiron.liu
Member
 
Registered: Jun 2009
Location: Harbin
Posts: 32

Original Poster
Rep: Reputation: 0
Thank you~
Code:
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
export PATH
That two statement in "MySQL" daemon start/stop script. When MySQL started but "$basedir/bin" not append to PATH. The reason is same too?
 
Old 06-20-2010, 04:41 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

In short: Yes.

You need to add the full path. I.e.:

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/my/own/bin.

Hope this helps.

EDIT
Also: don't use $PATH in these startup scripts (as in PATH=$PATH:../../..), this variable isn't present/set when your box boots.
/EDIT

Last edited by druuna; 06-20-2010 at 04:45 AM. Reason: Added a heads-up.
 
  


Reply


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
Is it possible to set an environment variable name containing a period in BASH? dnwake Linux - General 7 03-28-2010 02:05 PM
how to set environment variable with a given script sonu kumar Linux - Newbie 2 11-23-2009 05:52 AM
How to set the environment variables in BASH shell (RHEL) madhusudaniyaar Linux - Newbie 2 06-04-2009 05:41 AM
how to set environment variables in bash profile praveenkorukonda Linux - Newbie 1 02-22-2007 12:40 AM
Bash script environment variables mbjunior99 SUSE / openSUSE 4 12-28-2005 01:40 AM


All times are GMT -5. The time now is 11:07 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration