Reputation: 417
How can I set the new environment variables and their value permanently in Linux?
I used export
to set the environment variables. But the problem is its session specific. If I open a new session, the values set will be gone.
Upvotes: 17
Views: 60732
Reputation: 1
To create an environment variable from a Bash shell script:
declare -x [variable_name]=[variable_value] works well...
Example:
declare -x MY_VARIABLE=value
to check, go to another logged in terminal session and type:
env | grep MY_VARIABLE ; this will confirm it has been created and
; is accessible by other processes
Upvotes: 0
Reputation: 3082
If you want to set a variable in your shell (as opposed to the superuser's):
.bashrc
existsls -A ~/.bashrc
export VARIABLE=value
$ echo $VARIABLE
value
Keep in mind this depends on your shell.
Upvotes: 23
Reputation: 408
This is working inside Debian 11. It should work on other Debian based distributions like Ubuntu etc. I am using the old school nano to edit the file named pam_env.conf
that resides in /etc/security/
directory, you may use whatever else you want instead of nano.
sudo nano /etc/security/pam_env.conf
Format for setting the environment variable inside this file is following:
VARIABLE [DEFAULT=[value]] [OVERRIDE=[value]]
(For example, let's set DXVK_HUD
variable with the value full
. This is equivalent to export DXVK_HUD=full
You can replace the variable name and it's value to whatever you want for your use cases.)
This is how it is going to look inside that file as a new line:
DXVK_HUD DEFAULT=full OVERRIDE=full
Make the changes by saving it ( CTRL+O key combination is used for making changes through nano). Then press return (Enter key). And then press CTRL+X to exit from nano.
Restart your system. And type env
in Terminal and see if you can see your environment variable in the list. It should be there.
Upvotes: 2
Reputation: 14527
Solution: In order to export and keep environment variables persistent on linux you should export variables decelration into one of the following files: ~/.bash_profile
/ ~/. bash_login
/ ~/.profile
.
When bash is invoked as an interactive/non-interactive login shell, it first reads and executes commands from the file /etc/profile
(if exists) and after looks for these following files ( in that order and do the same) ~/.bash_profile
, ~/. bash_login
, ~/.profile
.
Example: adding secret token into my user profile.
cat << End >> ~/.profile
export SECRET_TOKEN=abc123!@#
End
output:
echo $SECRET_TOKEN
abc123!@#
Upvotes: 4
Reputation: 3797
Set it in /etc/environment
. In my Ubuntu installation this is the place where you can set environment variables persistently. The file may be different for a different distribution. Following is the contents of my /etc/environment
file.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
See how the environment variable PATH
is being set above.
A note on export command
export varname
makes variable varname
available to any subshell run from the your current shell, i.e. the shell on which you ran the export
command. Any other shell, which is either unrelated to your current shell or is a parent will not have this variable. Knowing this, assuming you are using bash shell, you can write the export command in .bashrc
file. .bashrc
is a file which is run every time a bash shell is started, and hence any command you write in it gets executed in any bash shell opened. Thus writing the export command in the .bashrc
file is another option. Similar will be the process for any other shell you are using. for eg. for Z shell the file is .zshrc.
Upvotes: 1
Reputation: 134
there's a good explanation of when to put it where here : http://www.linuxfromscratch.org/blfs/view/6.3/postlfs/profile.html if you don't have root access put it somewhere local like .bash_profile or depending on which shell you use. Find your shell by typing the command ps .
Upvotes: 1
Reputation: 4236
The usual place is ~/.bashrc
assuming that you're using bash, which is the default in most distributions. Check yourself with echo $SHELL
.
You may use ~/.bash_profile
, if you only want to set the variable in login shells (but i.e. not in scripts).
Upvotes: 2