News
Seven days of Black Friday madness — great deal from Serverspace!
Serverspace Black Friday
RF
August 11, 2023
Updated August 11, 2023

Environment variables in Linux

Debian Maintenance VPS

Introduction

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

  • Data Handling: Variables serve as containers in memory to store and manipulate data, enabling programs to work with values throughout their execution;
  • Dynamic Adaptability: By utilizing variables, programs achieve dynamic behavior, allowing them to flexibly adjust to various inputs and conditions;
  • Readable and Maintainable Code: Assigning meaningful names to data using variables enhances code readability, making it easier for developers and maintainers to understand and work with the code;
  • Code Reusability: Variables promote code reusability by storing data in one place that can be referenced and reused multiple times, reducing redundancy and promoting efficient programming practices;
  • Parameter Passing: Crucial for data exchange within a program, variables facilitate passing information between different parts of a program or functions, enabling functions to receive input and return results to the calling code;
  • Configuration Management: Environment variables, in particular, are valuable for storing configuration settings and systemwide information, providing a convenient method to modify program behavior without altering the source code.

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
Creation local environment variables
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
Deletion local env variable
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
Find configuration
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
Open file
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
Result
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
Write global 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.

Result
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.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.