Solved

Can't set enviroment variables inside bash script

Posted on 2009-04-14
10
599 Views
Last Modified: 2013-11-15
I'm having problems persistently setting environment variables in a bash script. Here's an example of the script :

cat > /etc/environment << EOF
TEST="TEST"
EOF

source /etc/environment

The problem is these variables become accessible to to the script that runs but once the script is complete they aren't accessible to the shell. Please note that this script is running as user-data on my Amazon EC2 server so I have no control over how the script is launched.

Is there a way to source these variables so I can then access them from the shell? Also please note my original script places a lot of variable in /etc/environment

0
Comment
Question by:TotallyMe
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 24144811
From the shell, type

. /etc/environment

or put it in your profile, eg: $HOME/.bash_profile
0
 

Author Comment

by:TotallyMe
ID: 24144857
Sorry neither of these will work, I have to do it from within the script to meet my needs. For example another bash script may need to run and make use of these environment variables. There must be a way to do this from within the script.

I found this http://www.perlmonks.org/?node_id=273841 but... eekkk... can anyone make sense of it?
0
 
LVL 84

Expert Comment

by:ozo
ID: 24145516
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 24145727
You need to export the variables in the script that you're sourcing:

cat > /etc/environment << EOF
export TEST="TEST"
EOF

0
 
LVL 48

Expert Comment

by:Tintin
ID: 24145794
exporting variables isn't going to work if the other scripts that run aren't children of the script that exports the variables.

Your only choice is to add

. /etc/environment

to whatever scripts you want to run
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:Morne Lategan
ID: 24145967
source /etc/environment is the same as . /etc/environment. Just alternate notations.

Therefore, if you source the script, or use the alternate notation, and you export it, all scripts thereafter will see the variables. Try it :)
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 24146006
Not so smart moment there, I get what you're saying Tintin :) Source the original script as well, or put the source /etc/environment bit in /etc/profile so that its sourced at logon.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 166 total points
ID: 24146017
only if they are children or descendants of the script that did the source
0
 
LVL 7

Accepted Solution

by:
Morne Lategan earned 167 total points
ID: 24146171
True, @ozo and @Tintin. My bad.

The suggestions in my previous post to source the original script, or source the environment at logon should work as an alternative to putting the source in each script that requires the variable to be set.
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 167 total points
ID: 24148721
env variables if exported can be seen by the child shell (which will execute the script) and any changes to these env variables (in value) are not seen by parent shell. Env variables set or created in child process are not seen by parent. Thus, set all env variables in the login shell by sourcing the file, for the scripts run to see these env variables
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
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.

707 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

18 Experts available now in Live!

Get 1:1 Help Now