LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 09-03-2003, 12:40 AM   #1
galalleni
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Rep: Reputation: Disabled
Question BASH export command


Howdy,
My shell script enviroment variables are not exported (yes, I used export) to the parent shell. This is true of root and user accounts. Although, /etc/profile script env. vars export correctly. This occured after I upgraded to Slackware 9.0.

Here is the sample script I am debugging:
Code:
#!/bin/bash
# sample.sh Foobar script
#
FOOBAR="Hello World"
export FOOBAR
Then I try,
Code:
somebody@network:~# ./sample.sh
somebody@network:~# echo $FOOBAR

somebody@network:~#
, but this works:

Code:
somebody@network:~# FOOBAR="Hello World"; export FOOBAR
somebody@network:~# echo $FOOBAR
Hello World
somebody@network:~#
Yes, my script has exec permission. I am hoping I don't have to recompile bash (like I did for ISC dhclient, because something is broken in the package - I had to use the source tarball from ISC). And many hours/google searches later I ended up here. Any Ideas?

Thanks in adv. for any help (/sympathy)!!
 
Old 09-03-2003, 02:29 AM   #2
arakno
LQ Newbie
 
Registered: Jul 2003
Distribution: Slackware
Posts: 10

Rep: Reputation: 0
I may be wrong but I think enviroment variables in bash scripts are supposed to be used within the script.

Here, to setup 'global' bash variables, I use ~/.bash_profile

Example:

Code:
export CHARSET=ISO8859-1
 
Old 09-03-2003, 05:08 AM   #3
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Run:
. ./sample.sh

dot first !
 
Old 09-06-2003, 11:20 PM   #4
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
The export command makes the value of a variable available to the current process and to subprocesses, but not to parent processes!

When you create a shell script and execute it, a new shell process is started to execute the contents of that script. Any exported variable values are available to the new shell, or to any subprocesses the new shell creates. When the script ends, none of the exported variable values are available to the parent process (your login shell).

When you log in, the first thing that the login shell does is runs your login script (e.g. ~/.bash_profile), which is why any exported variable values in that script are available to you from that point on.

Adding the . before you execute sample.sh (for example: . ./sample.sh) will work. The . is a shell builtin command that causes the script to be read and executed in the current shell process rather than starting a new shell process to do that. That is why exported variable values are available to your login shell. You can do the same thing with "source ./sample.sh" since source and . mean the same thing.
 
Old 09-08-2003, 12:44 AM   #5
galalleni
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you! I forgot about the source command (I need to brush up on my BASH scripting).
 
  


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 call in bash script Donald1000 Linux - Software 10 03-12-2009 08:03 PM
setenv - bash - export allelopath Linux - General 4 04-17-2005 05:38 AM
set and export using bash script acummings Linux - General 10 01-03-2005 02:22 PM
sticking export $LFS in bash file glock19 Linux From Scratch 7 01-29-2002 06:58 PM
BASH variable export Barbarian Programming 2 11-27-2001 08:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 10:45 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
Open Source Consulting | Domain Registration