Solved

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

Posted on 2002-03-12
8
341 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now