setting up persistent environment variables in Linux
How to setup persistent environment variables in Linux shell?
We often need to set environment variables in Linux shells. If we just “export” them on the shell, they disappear in next session. In order to set them permanently follow this article.
Setting up temporary environment variables that are valid for a single session.
In this example, I am setting an environment variable called DAY with value “Wednesday”
[root@electronproton ~]# export DAY="Wednesday" [root@electronproton ~]# echo $DAY Wednesday
But this variables disappears once we exit the shell.
Setting up persistent environment variables for an user account
To setup persistent variables you have to put the variables in shell’s init file. Each shell has its own init file as described below. When a shell is started it first reads init files and executes commands in it before executing any other user command. So, the variables get exported every time you start a new shell.
To know which shell you are using, do as follows:
[root@electronproton ~]# echo $SHELL /bin/bash
I am using “bash” shell. So, I have to set variables in “$HOME/.bashrc” file
Shell Name | Init File |
bash | .bashrc |
csh | .cshrc |
ksh | .kshrc |
zsh | .zshrc |
tcsh | .tcshrc |
Step 1: Use a text editor like “vi” and edit init file.
#vi $HOME/.bashrc
Step 2: At the end of the file append the following line
export DAY="Wednesday"
Step3: Save the file and exit.
Step4: source the variables
#source $HOME/.bashrc
Note that these are valid only for a particular user account. If another user logs in, they cant find these variables.
Setting up persistent environment variables system-wide.
To setup system-wide environment variables that are applicable for all user accounts you have to update them in “/etc/profile” file.
Step1 : Edit /etc/profile
[root@electronproton ~]# vi /etc/profile
Step 2: Append environment variables at the end of file
Step3: Save and exit
Hope this helps 🙂