Solved

Can't set enviroment variables inside bash script

Posted on 2009-04-14
10
605 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Block sender e-mail address in Postfix 4 65
nagios remote hosts 9 43
Centos 7 User to Start Apache, MariaDB, LDAP 5 26
networking details on centos 6.6 4 50
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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 …
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.:
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.

911 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

23 Experts available now in Live!

Get 1:1 Help Now