Solved

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

Posted on 2002-03-12
8
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

690 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