Solved

Can't set enviroment variables inside bash script

Posted on 2009-04-14
10
608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
9 textboxes: is my password wrong. wrong port? 10 69
SSH setup on ASA 5505 17 127
Windows 10 linux VM 30 93
Edit smb.conf-file in Samba om a ReadyNAS 3 76
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 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 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.:
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…

710 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