0

I have a .sh file that locally sets some environment variables in my shell source my_env.sh.

#!/bin/sh
echo "Setting up local environment variables"
export MY_URL="http://domain.com"
echo "Done"

This only sets the variables for that session. Which means that my Python Celery and Supervisor apps which run under a different session cannot access them. I understand I can run them under a user, but I want to know if on Ubuntu using the same shell script above if I can set the variables so they are globally accessible to all applications regardless of users or session?

3 Answers 3

3

export your variables in the "/etc/profile".

NOTE: This will make it global for each shell sessions for any user. If you wish to set this variable for every session for a specific user, set it in that user's "~/.profile".

Sign up to request clarification or add additional context in comments.

1 Comment

So would I need to add a file to this folder?
1

According to Ubuntu env variables doc the best way would be

A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment

That's assuming you don't mind having them set for the whole machine.

6 Comments

cd /etc/environment is not a directory on Ubuntu 14
/etc/environment is not a directory but a file. So open it and add the following line: MY_URL="...."
Is there a way my shell script can be executed to copy these variables to that location?
Depends on how your stuff is run. According to the doc: "Files with the .sh extension in the /etc/profile.d directory get executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh". So if you enter your session from a shell, both will do and profile.d/something would be nice too. But you have to open a session via ssh or login.
I was thinking more of running the script once and it added the variables to environment files i.e. echo out
|
0

If you are using bashrc or zshrc you can source the shell file which sets the environment variables across the sessions or precisely loads all the environment variables in each session.

source location/shell-script-sets-env.sh  

The zshrc and bashrc is available in your $HOME directory. or ~/.zshrc and ~/.bashrc. The current shell can be looked via

echo $SHELL 

Have a look at setting env permanently for adding it to the profile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.