How to permanently export a variable in Linux?
You can add it to your shell configuration file, e.g. $HOME/.bashrc
or more globally in /etc/environment
.
After adding these lines the changes won't reflect instantly in GUI based system's you have to exit the terminal or create a new one and in server logout the session and login to reflect these changes.
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/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.
Notes
/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.
~/.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 includingHOME
orPATH
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 extendingPATH
with values such as~/bin
or~/go/bin
or defining user specificGOPATH
orNPM_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.
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
Related
- Ubuntu
- Arch Linux
Related
Difference between Login Shell and Non-Login Shell?
Environment variable change in shell but won't export
Some terminal emulator doesn't run new tabs as login shell. E.g. in Gnome Terminal You should:
- List item
- Go to Edit -> Profile Preferences.
- Select the Title and Command tab. Notice how the Run command as login shell checkbox is unchecked! Check it.
Furthermore setting a variable in a shell session does not make it permanent for later sessions. E.g. exporting a variable makes it available for any further processes that's got created from the actual session.
To make it somewhat permanent You have to add it to e.g. .bashrc
Do note:
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
Quoted from the Ubuntu help.
So to decide where to add it please read the fine manual
Related Topics
How to Redirect Output of an Already Running Process
How to Use 'Cp' Command to Exclude a Specific Directory
How to Run a Shell Script on a Unix Console or MAC Terminal
How to Write Data to Existing Process'S Stdin from External Process
Find -Exec' a Shell Function in Linux
Linux Bash: Multiple Variable Assignment
How to Syntax Check a Bash Script Without Running It
Get Exit Code of a Background Process
How to Split CSV Files as Per Number of Rows Specified
What Is a .Pid File and What Does It Contain
How to Loop Over the Output of a Shell Command
How to Find the Original User Through Multiple Sudo and Su Commands
Externalizing Tomcat Webapp Config from .War File
Linux/Unix Command to Determine If Process Is Running
Core Dump File Is Not Generated