How to set global environment variables using shel

2020-04-01 10:15发布

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条回答
地球回转人心会变
2楼-- · 2020-04-01 10:26

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

查看更多
Evening l夕情丶
3楼-- · 2020-04-01 10:26

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.

查看更多
放荡不羁爱自由
4楼-- · 2020-04-01 10:31

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.

查看更多
登录 后发表回答