Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can't set enviroment variables inside bash script

Posted on 2009-04-14
10
Medium Priority
?
615 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 85

Expert Comment

by:ozo
ID: 24145516
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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
 
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 85

Assisted Solution

by:ozo
ozo earned 664 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 668 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 668 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses
Course of the Month7 days, 17 hours left to enroll

824 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