There are multiple ways to do it. The actual solution depends on the purpose.
The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In the case of the shell script, you must use a specific shell syntax and export or set commands. You also need to consider for which situations you are setting the environment variable, whether for Login/Non-login shell or Interactive/Non-interactive shell.
System wide:
/etc/environment List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. Used by PAM and systemd.
/etc/environment.d/*.conf List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). Used by systemd that by design do not pass those values to user login shells.
/etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for PATH entries that are valid for every user like /usr/local/something/bin. The file is included by other script so use POSIX shell syntax not the syntax of your user shell.
/etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells in login mode.
/etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
User session:
~/.pam_environment. List of unique assignments, no references allowed. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variables including HOME or PATH so it has limited use. Used by PAM.
~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending PATH with values such as ~/bin or ~/go/bin or defining user specific GOPATH or NPM_HOME. The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.
~/.profile, ~/.<shell>_profile, ~/.<shell>_login Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.
~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
## -------------------- ## Environment variable ## -------------------- ## Persistant across current session (all process of current session) echo $$ # returns your current PID local_var="i m local" global_var="i m global" export global_var # set global_var as environment variable, export global_var="i m global" also works printenv global_var # check if global_var is set as environment variable, returns i m global echo $local_var # returns i m local echo $global_var # returns i m global bash # forks a child process echo $$ # returns your current PID echo $local_var # returns nothing as local_var is not available to this new child process echo $global_var # returns i m global exit # return to original process exit # exit the current user session and login again echo $global_var # returns nothing as export global_var is not persistant ## Persistant across current user (all sessions of current user) echo 'global_var="i m global"' >> ~/.bash_profile ## add the environment variable to user's session wide configuration # you can also add in ~/.profile as well which will be availble to all type of shell including bash for current user echo $global_var # returns nothing as the variable will be exported the next time user logged in source ~/.bash_profile # immidiate effect echo $global_var # returns i m global exit # exit the current user session and login again echo $global_var # returns i m global, environment variable is now persistant accross current user session sudo -i # login as root, any other user will do echo $global_var # nothing as the environment variable is persistant in the previous users environment only exit # back to original user ## Persistant across system (all process of all sessions of all users) echo 'export global_var="i m global"' | sudo tee /etc/profile.d/global_var.sh ## add the environment variable to system wide configuration # can be added to /etc/environment as well which will be available in all shell including bash echo $global_var # returns i m global sudo -i # login as root, any other user will do echo $global_var # returns i m global, environment variable is now persistant accross system exit # return to current session |