Linux Advanced - Environment Variables in Linux

Linux Advanced - Environment Variables in Linux

Anyone having a little bit of programming knowledge will have an idea about what a variable is!

A variable is a symbolic name for a value it contains. In more technical terms we can say it's a storage location (identified by a memory address) and mostly denoted by English/Greek letters or symbols (can be a filename, text, number or any other data).

variable-1.png

In this tutorial we are going to learn about Environment variables in Linux.

What are Environment Variables?

When you login to a Linux system a shell environment gets setup for the user logging in. Bash environment variables defines the environment it creates when it launches. The information they carries and applies is your username, locale, history file size, your default editor, and a lot of other things. They too are defined by a key:value pair as shell variables.

Syntax: variable_name=value

For example: HOSTNAME=lco-worker2.example.com

A variable can have more than one value and will be separated by a colon: variable_name=value_1:value_2

Quotation marks will be used if the variable has values with spaces in between: variable_name="text value"

Environment variables are available system-wide and they will be subsequently inherited by all spawned child processes and shells whereas the shell variables only applies to the current shell instance.

List all environment variables

You can list all environment variables in the shell by running following command:

[root@lco-worker2 ~]# env

env_variables_list.png

There is another command printenv. It prints the values of the specified environment VARIABLE(s). If no VARIABLE is specified, it prints name and value pairs for them all.

printenv.png

Accessing the environment variables

Environment variables can be accessed by running any of the following commands.

If you know the variable name then just run the following command. echo $variable_name

Do not forget to add $ sign before variable name while accessing any variable's value.

For e.g.

[root@lco-worker2 ~]# echo $HOSTNAME
lco-worker2.example.com

You can either grep the variable and its value by running following command. set | grep variable_name

For e.g.

[root@lco-worker2 ~]# set | grep HOSTNAME
HOSTNAME=lco-worker2.example.com

The set command is used to confirm variable creation.

How to Set an Environment Variable in Linux

There are two methods to set an environment variable.

Setting non-persistent environment variables

Use export command to set environment variable which will be set for the current shell session only aka non persistent.

[root@lco-worker2 ~]# export tutorial_name=env_variables

Here variable name is tutorial_name and it carries value env_variables.

Setting persistent environment variables

If you want to set an environment variable which should be persistent even after you close the shell session you need to create/modify few files.

Set it permanently for a single user ->

To set it for a single user, you need to edit the .bashrc file and add lines for each variable you wish to add in following format:

export variable_name=value

For e.g.

[root@lco-worker2 ~]# echo "export tutorial_name=env_variables" >> ~/.bashrc

[root@lco-worker2 ~]# echo $tutorial_name

[root@lco-worker2 ~]# logout

[root@lco-worker2 ~]# echo $tutorial_name
env_variables

Once you set the variable in .bashrc file in order to verify it's functioning you can logout and login back and try accessing the variable value.

persistent_env_variable.png

Set it permanently for all the users ->

Currently the tutorial_name environment variable is only set for root user and not for others.

single_user_set.png

If you want to set it for all the users create an .sh file in the /etc/profile.d directory. Add your content, save and exit the file. The changes will be applied at the next logging in.

[root@lco-worker2 profile.d]# touch my_env_var.sh

[root@lco-worker2 profile.d]# echo "export tutorial_name=env_variables" >> my_env_var.sh

[root@lco-worker2 profile.d]# logout

[root@lco-worker2 ~]# echo $tutorial_name
env_variables

[root@lco-worker2 ~]# sudo su - vagrant
Last login: Thu May  6 16:11:58 UTC 2021 on pts/0
[vagrant@lco-worker2 ~]$
[vagrant@lco-worker2 ~]$ echo $tutorial_name
env_variables

set_for_all_users.png

If you want to apply or make the environment active without logging out from the session run the following command.

[root@lco-worker2 ~]# source /etc/profile.d/my_env_var.sh

[root@lco-worker2 ~]# echo $tutorial_name
env_variables

source.png

Unset an Environment Variable

If you want to unset an environment variable there is a command called unset to do this.

unset variable_name

This will permanently remove the variables exported or set via a terminal command.

The variables which are stored in configuration files (.bashrc or individual .sh files in etc/profiles.d directory) will also be removed from the current shell session. However, they will be set again once you login back next.

[vagrant@lco-worker2 ~]$ echo $tutorial_name
env_variables

[vagrant@lco-worker2 ~]$ unset tutorial_name

[vagrant@lco-worker2 ~]$ echo $tutorial_name

[vagrant@lco-worker2 ~]$ logout

[root@lco-worker2 ~]# sudo su - vagrant
Last login: Thu May  6 16:25:38 UTC 2021 on pts/0
[vagrant@lco-worker2 ~]$ echo $tutorial_name
env_variables

unset.png

Some important environment variable

Below are few environment variables which one should be aware of.

env_list.png

That's all for this tutorial. I hope by now you will be comfortable dealing with environment variables on a Linux system

Hope you like the article. Stay Tuned for more.

Thank you. Happy learning!

Did you find this article valuable?

Support Learn Code Online by becoming a sponsor. Any amount is appreciated!