Solved

Can't set enviroment variables inside bash script

Posted on 2009-04-14
10
606 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
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…

813 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