Solved

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

Posted on 2002-03-12
8
358 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need BIOS update Linux for MSI X99A motherboard. 4 53
VMware Tools Install On Linux Problem 3 82
linux SFTP 8 60
Apache LDAP Authentication 20 26
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Fine Tune your automatic Updates for Ubuntu / Debian
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 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…

809 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