11.08.2023

Environment variables in Linux

Introduction

Variables, including environment variables, are fundamental elements in computing with numerous critical roles:

Environment variables, play a pivotal role in programming, enabling data storage, program behavior adjustment, and code readability. They are indispensable in software development and system administration, ensuring efficient and adaptable computing practices.

Type of environment variables

There are three types of environment variables and divide by scope of applying in the entire operating system. First of all, we consider local environment variables. They represent cell of memory that create by current session and don't write into the configuration files. Instead of this they storage in the cache memory and delete by event of close connection and log out by profile via special commands.

If you want to continue use that variables or you needed to save processes in work status, you can use screen utility. They make virtual terminal and allows to detach of system. Let's have a look how to manage that type of variables: for create, delete and change.

export VARIABLE_LABEL=value

Also you can display variable by the command below:

echo $VARIABLE_LABEL

Screenshot №1 — Creation local environment variables

That command set local environment variable for current session, however, highlight scope of usage that cell of memory. If you set their in utility or in program that will available in the accordance process. When the process terminates, the variable also ceases to exist, and its values are not passed to other processes or new sessions. If you want to change value, you can do it by using the same command as we previous described:

export VARIABLE_LABEL=new-value!

But if you want to delete this variable you need to type this:

unset VARIABLE_LABEL

Or you can assign empty field for simplify deletion:

VARIABLE_LABEL=

However, imagine a situation, that you need to delete for some time you variable and back value as we see before. For that we can run utility, shell and any process with out variables:

 env -i bash

Screenshot №2 — Deletion local env variable

Let's scale range of usage and look at the user environment variables, which implement and save every session, in comparison with local environment variables, for one user. Schema of process is similar enough, but now we write that command in the file, which start every time as we login in the system.

So, it's automize process of starting command in the file and make that more simplicity. We can work with two main files: .bashrc which use for the local user configuration and .bash_profile which we use for the remote user connection through SSH protocol. For the last one if they will unavailable at any reason, then system will search .bash_login and .profile. We can find that file by command below:

find . -name bashrc

Screenshot №3 — Find configuration

If you can't find needed file of remote connections variables for user,try to search .bash_login and .profile. Let's edit file and add needed raw:

nano ~/.bashrc

Screenshot №4 — Open file

And add needed variable in the file by the command below:

export GG='Nice to meet you!'

Save file with command after close text editor:

source ~/.bashrc

Screenshot №5 — Result

And last of the considering environment variable it's global, we highlight and remind that names and labels of variables indicate scope of usage. Currently scope covers the entire system from any users. We can edit their file bash.bashrc, environment, profile. They also divide into the several point for specific type of user connection. For example we can modify environment and that affect to all users, in the system independent of their type of connection:

nano /etc/environment

Enter the row below and save file with button combinations Ctrl+O:

export gg='Jay's dog byte me! '

Applying changes in the by the command:

source /etc/environment

Screenshot №6 — Write global environment variable

For only local connection user we can configure bash.bashrc with the same command for enter:

export gg='Jay's dog byte me! '
source /etc/bash.bashrc

For only remote connection user with range all system, we can modify profile also using the same command:

export gg='Jay's dog byte me! '
source /etc/profile 

For deletion that variables we need consequently delete that variables from all files in which we added that or use unset command as we described before.

Screenshot №7 — Result

And how you can see we don't displayed after that anymore.

Conclusion

The environment variables play a crucial role in computing, offering various benefits to software development and system administration. They serve as containers to store and manipulate data, enable dynamic behavior in programs, enhance code readability, and facilitate code reusability. Additionally, environment variables are vital for parameter passing and configuration management, providing a convenient method to modify program behavior without altering the source code.