#Linux

How to set environment variables in Linux permanently?

Gaurav BhardwajGaurav Bhardwaj
UPDATED 14 October 2021
How to set environment variables in Linux permanently?

In this article, we will learn how to set environment variables in Linux permanently. There are different shells available for Linux like bash, zsh, bourne, ksh, csh or tcsh In order to set an environment permanently, one thing is clear we need to write it in a specific file so that it will be persistent.

Now we will understand basically in which file we need to add our environment variables. For this, we need to deep dive into different shells:

1. Login Shell:

The login shell runs as a part of the login of the user to the system. Typically when you enter the CTRL + ALT + F1 key you will enter a virtual terminal or SSH to a different machine i.e. a login shell.

Also, If you run a login shell it executes a number of files on startup. This can influence how your system behaves and you have to put your environment variables in these files. The files that run in order are:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

First /etc/profile is checked for existence, if it exists then it is referenced, otherwise ~/.bash_profile is checked for existence and if it exists then it is referenced, otherwise ~/.bash_login and so on till ~/.profile

2. Non-login Shell:

Any other shell that runs by the user after logging on, or which is run by any automated process which is not tied to a logged-in user. When you open any terminal from your Linux distro like gnome-terminal i.e a Non-Login shell.

3. Interactive Shell:

Interactive means that the commands are run with user interaction from the keyboard. Let's say when you log in to a system, then you specified a shell to be executed say bash. That is an interactive shell.

root:~$ bash

An interactive (bash) shell executes the file .bashrc so you have to put any relevant variables or settings in this file.

4. Non-interactive Shell:

A non-interactive shell is a shell that can not interact with the user. It's most often run from a script. This means that .bashrc and .profile are not executed. Non-interactive shells often influence your PATH variable.

NOTE: It is always a best practice to use the full path for a command but even more so in non-interactive shells.

Now, You might have one question, How to identify you are on Login shell or Non-Login shell?

Simply open a terminal and execute the below command.

root:~$ echo $0

If it returned a result with a hyphen then you are in a login shell & when it doesn't return a result with a hyphen then you are in non-login shell.

How to set environment variables in Linux permanently
Non Login Shell

There might be some scenarios, if you log in to bash using a terminal or terminal emulator like putty, then the session is both a login shell and an interactive one and If you then type bash then you enter an interactive shell, but it is a non-login shell. If we run a shell script, then it is neither a login shell nor an interactive one but a non-interactive shell.

I am confident now it is clear to you what is the theory about shell is & which file you should write your environment variables to. We have provided some of the examples below.

How to set environment variables in Linux permanently

Login Shell

bash

vim ~/.profile
#You can write this lines in the bottom of the file 
export VARIABLE_NAME=PATH_NAME

zsh

vim ~/.zprofile
#You can write this lines in the bottom of the file 
export VARIABLE_NAME=PATH_NAME

bourne

vim ~/.profile
#You can write this lines in the bottom of the file 
VARIABLE_NAME=PATH_NAME    
export VARIABLE_NAME

csh or tcsh

vim ~/.login
#You can write this lines in the bottom of the file 
setenv VARIABLE_NAME=PATH_NAME 

ksh

vim ~/.profile
#You can write this lines in the bottom of the file 
export VARIABLE_NAME=PATH_NAME

Make sure to re-login the user, to apply the changes.

Example:

I am currently in zsh (non-login), as the output of echo $0 is not having hyphen. Now I will export the environment variable $WORD in ~/.zprofile as shown in the image below. Now, I need to re-login the user and check the variable value in the login shell.

How to set environment variables in Linux permanently?

After, I logged in to the Login shell, the value is coming right as Jojo.

How to set environment variables in Linux permanently?

Non-Login Interactive Shell

Bash

vim ~/.bashrc
#You can write this lines in the bottom of the file 
export VARIABLE_NAME=PATH_NAME

Zsh

vim ~/.zshrc
#You can write this lines in the bottom of the file 
export VARIABLE_NAME=PATH_NAME

Make sure to restart the shell, to apply the changes.

Example:

In the non-login interactive mode, I will simply add the environment variable to ~/.zshrc and will restart the shell.

How to set environment variables in Linux permanently?
How to set environment variables in Linux permanently?

I hope you are now well versed with setting the environment variables. If you have any queries or suggestions please feel free to write in the comments section below. If you are looking for the most useful commands of Linux for developers you can refer to this article. Thanks.

Comments#0

Leave a Comment

User