Shell Script to Set Environment Variables Permanently

How do I permanently add an environment variable?

Edit your ~/.bashrc file and place at the end:

export JAVA_HOME=jdk-install-dir
export PATH=$JAVA_HOME/bin:$PATH

You can even modify /etc/environment if you want it to persist for all users.

Issue with setting environment variables permanently in linux bash

~/.bash_profile is only sourced on login (i.e. after you've typed your username & password) - ~/.bashrc is sourced for interactive non-login shells.

So I'd add the variables into ~/.bashrc (don't forget to source it first if you're running the python script afterwards from the same shell). This way, when you open a new shell, bashrc will be sourced and your environment variables will be available.

Edit:
As others have said in comments .. running an export command in one shell, won't make that variable availble in another shell - you need to add it to your ~/.bashrc to make it avaiable in other shells

How to permanently set $PATH on Linux/Unix

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 case of the shell script you must use a specific shell syntax and export or set commands.

System wide

  1. /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.

  2. /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.

  3. /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.

  4. /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.

  5. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.

User session

  1. ~/.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.

  2. ~/.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.

  3. ~/.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.

  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.

Notes

GNOME on Wayland starts a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile, ~/.<shell>_profile, ~/.<shell>_login files.

Man pages

  • environment
  • environment.d https://linux.die.net/man/1/environment.d
  • bash
  • dash

Distribution-specific documentation

  • Ubuntu
  • Arch Linux

Difference between Login Shell and Non-Login Shell?

How to programmatically set a permanent environment variable in Linux?

LSB-compliant (see spec) practice is to create a shell script in /etc/profile.d/ folder.

Name it after your application (and make sure that the name is unique), make sure that the name ends with .sh (you might want to add scripts for other shells as well) and export the variables you need in the script. All *.sh scripts from that directory are read at user login--the same time /etc/profile is sourced.

Note that this is not enforced by bash; rather, it's an agreement of sorts.

how to set environmental variables permanently in posix(unix/linux) machine using python script

Here is what I use to set environment variables:

def setenv_var(env_file, set_this_env=True):
env_var = "LD_LIBRARY_PATH"
env_path = "/usr/local/lib"`enter code here`
# set environments opened later by appending to `source`-d file
with open(env_file, 'a') as f:
f.write(os.linesep + ("%s=%s" % (env_var, env_path)))
if set_this_end:
# set this environment
os.environ[env_var] = env_path

Now you only have to choose where to set it, that is the first argument in the function. I recommend the profile-specific file ~/.profile or if you're using bash which is pretty common ~/.bashrc

You can also set it globally by using a file like /etc/environment but you'll need to have permissions when you run this script (sudo python script.py).

Remember that environments are inherited from the parent process, and you can't have a child set up a parent process' environment.



Related Topics



Leave a reply



Submit