Solved

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

Posted on 2002-03-12
8
351 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access_log 17 100
App holding yum lock unable to update my rpm package 1 36
Linux  Script File Attributes 5 53
Python variable _ manually assigned 9 65
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…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

920 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

14 Experts available now in Live!

Get 1:1 Help Now