Environment Variables on Linux Based Systems.

Hi guys

In this blog post, I will be trying to tell you which is about what is the environment variables and how to create on linux/unix based systems. I just learned and I thought that I should write a blog post and share with people. By the way, if I need this in the future, I can look from here.  🙂


What is Environment Variable?

In each system, there are some variables that are related to their own paths and their values. The operating system environment variables are variables that can be configured. These environment variables usually contain program locations, some mostly used information and settings. As we can use variables in programs, shell scripts etc. also we can create our own environment variable.

Environment variable often to be a (string) character.

For example, “NAME” can be an environment variable, and “ucribrahim” can be value of the environment variable. You will be understanding in the next parts of this article that what I’m trying to tell you with this example.


Environment Variable & Example Commands

There are some systems variables which is predefined on unix/linux based systems. Here they are.

  • BASH and SHELL
  • CPU
  • ENV
  • EUID and UID
  • HOME
  • MAIL
  • PATH
  • PWD
  • USER
  • TERM

Let’s do some examples which on widely-used variables. Commands are used on the latest version of the Centos operating system and on the other systems these commands can be different.

Use the following command to learn current user’s bash name. Both of this commands are okay.

root# echo $BASH

root# echo $SHELL

We can learn the path that what we have used commands on the terminal at the past by using the following command.

root# echo $HISTFILE


An example similar to the example above is the value of how much of the commands saved which is used in the past.

root# echo $HISTSIZE


What is the current user’s home directory right now? I’m the root right now that is why the results is as follows.

root# echo $HOME


Using the following command, a command that you used when you run the command/program shows you what directory will look into. The commands will look into the directories by one by as follows. If it is found in one of the directories, the command will execute, otherwise it will not work.

root# echo $PATH


The other variable is that shows you where you currently at.

root# echo $PWD


The following command will show us which directory the user’s mailbox is in. The paths can be different in the other linux based systems.

root# echo $MAIL


Using following command to learn what kind of operating system are you using.

root# echo $OSTYPE


To view the type of the terminal which is user’s current terminal.

root# echo $TERM


If you want to learn user’s username, use following command. I’m in the root account, that’s why you’re seeing this (root) username.

root# echo $USER


Use the following command to see UID (user id) number of the current user.

root# echo $UID


ucribrahim# echo $UID



Define Environment Variables

We saw and learned the basic and used usually environment variables, now we know a little bit what are they and how to use. Now is we going to learn how to create our own environment variables and use it where we want at.

Firstly, let’s list all of the environment variables on system. You can also use “printenv” command.

root# env







LS_COLORS=rs=0:di=38;5;27:ln= ....






The environment variables are consists of two stages on linux based systems. For example, we want assign to “lifeoverlinux.com” value into the “site” variable. First stage, by this site=lifeoverlinux.com command that we create “site” variable on the current shell; In the second stage, the export site command converts the shell variable into an environment variable.

For example, Use the following command to create our own variable and to add value into it.

root# site=lifeoverlinux.com

And then we can see the variable with the following command which is we have created at past.

root# echo $site 


For example, by using “env” command to see all environment variables and we can see that we didn’t create our own environment variable. We just created a variable on the shell guys.

root# env | grep “site”

But if we use the following command, we can convert our variable to the environment variable and then we can see our environment variable by “grep” command when we list all environment variables.

root# export site

root# env | grep “site”


Let me show you guys a easiest way to create our environment variable with a single command. Use the following command to create an environment variable so easily.

export = Command help you to convert shell variable into the environment variable.

site = This is a variable name.

lifeoverlinux.com = This is the value of the variable.

root# export site=lifeoverlinux.com

Alright, if we want to delete environment variable which is we created at past. We can use the following command.

root# unset site 


Define Environment Variables Permanently

The commands we have described and applied above will be accessible until the session is terminated. We need to add our environment variable into the “.bashrc” file which is in the user’s home directory, if we want to do it permanently.

root# nano $HOME/.bashrc

Example .bashrc file.

You can then update the .bashrc file by using the following command and the file can be re-read by the system.

root# source $HOME/.bashrc

What we have done above applies only to our own users. If we want the users use our own environment variable, we can add to our environment variable into the /etc/environment file.

You can restart the system for the settings to take effect, or you can exit and re-enter the user shell.

root# nano /etc/environment

NOTE : The environment variables are in the case-sensitive. I mean “SITE” and “site” is not the same thing, both of them are different variables.

NOTE : For more information, you can look here.


Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *