Solved

Setting a shell variable from within a script file...

Posted on 2002-03-12
8
357 Views
Last Modified: 2010-05-18
Dear all,



I'm running Linux Mandrake 7.2 on a PC with the bash shell.


I would like to setup some variables in a shell script, so as to make these variables available to all subsequent shell commands.


Here is the shell script:

#!/bin/sh
export BEA_HOME=/mnt/windows/bea


The shell script is stored in a file called setenv.sh. To run the shell script I type in the command ./setenv.sh


Now if after having typed that command, I type in the command echo $BEA_HOME, I get an empty line.


So clearly, the variable BEA_HOME has not been set. Or rather it has been set, but not exported outside of the script shell.


Then I add the line echo $BEA_HOME to the shell script, obtaining the following shell script:

#!/bin/sh
export BEA_HOME=/mnt/windows/bea
echo $BEA_HOME


Now running the shell script will display the variable.


But if I then type in the command echo $BEA_HOME after the execution of the shell script, I still get an empty line.


The variable is not exported to the current parent shell.


Then I removed the first line of the shell script, obtaining the following shell script:

export BEA_HOME=/mnt/windows/bea
echo $BEA_HOME


And I got the same result. No better.


How could I export a variable to the current shell, from a shell script.



Any idea?
Stephane
stephaneeybert@hotmail.com

0
Comment
Question by:stephaneeybert
8 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 6856235
. ./setenv.sh
0
 
LVL 3

Expert Comment

by:tdaoud
ID: 6856239

Stephane,

You need to run the shell script with a dot "." infront so it will run within the current shell.  For example you need to run it using the command

. ./setenv.sh

Notice the first dot then the space.

This should do it for you.  Give it a try.

Tarik
0
 
LVL 3

Expert Comment

by:tdaoud
ID: 6856269
I appologize, it seems that I and ahoffmann submitted our answers at the same time.  I did not see ahoffmann's answer when I wrote mine.

Mine should be cancelled or not taken into consideration since it is the same as ahoffmann's solution.

Sorry about the Internet delay stuff.

Tarik
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 3

Expert Comment

by:tdaoud
ID: 6856277
I appologize, it seems that I and ahoffmann submitted our answers at the same time.  I did not see ahoffmann's answer when I wrote mine.

Mine should be cancelled or not taken into consideration since it is the same as ahoffmann's solution.

Sorry about the Internet delay stuff.

Tarik
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6856286
Tarik, don't worry, be happy ;-)
0
 
LVL 2

Expert Comment

by:mhonomichl
ID: 6856697
You need to run the script as stated above.  Just for future reference however, if you are setting variables, when you are using sh, you cannot set the variable and export it on the same line.  You have to set it, then export it:

# SHEBANG=/bin/sh
# export SHEBANG

if you are using bash, ksh, or zsh, you can do it all on one line:

# export SHEBANG=/bin/zsh

BTW:  you can set these in your .profile or .bashrc if this is something you would like all the time when you log in.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6856790
> .. in your .profile or .bashrc ..
take care when making such suggestion, bash is not that user-friendly when trying to find rc-files, you need to know how bash does it !
0
 

Author Comment

by:stephaneeybert
ID: 6858808
Thanks to you all!
Very instructive comments!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux installs 6 60
expectj telnet failing 5 36
reset ubuntu password for root and admin after clearing it. 7 44
linux 13 50
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question