You have been using variables since the start of your programming journey. You may have wondered if there are variables defined by the operating system or the environment on top of which other programs function. There are indeed such variables, and they are an essential part of the operating system. Let's have a look at environment variables.
Environment variables
Environment variables are variables set outside the usual computer program. This is done with the help of the operating system that allows us to define, edit, save, and use such variables in a much larger context. As the name implies, an environment variable is a variable made up of a name/value pair. Unlike variables that you used to define in code or terminal, environment variables may be accessible across the whole OS depending on its scope.
Variables in your program and the value they contain determine the functioning of your program. In a similar way, these environment variables determine the behavior of processes that run on your computer. Environment variables have different implementations and syntax across different operating systems.
How do environment variables work?
Each process has its own separate set of environment variables. The process acquires this set of environment variables from its parent process when it is created. The process will be able to use the values contained in these environment variables as required.
In Unix, the environment variables are initialized during system startup by scripts designated to run at startups. In Microsoft Windows, each environment variable's default value is stored in the Windows registry or set in the AUTOEXEC.BAT file.
Environment variables are case-sensitive in Unix and Unix-like systems. You can retrieve the value using the $ sign in front of the variable name. However, environment variables are not case-sensitive in DOS or Windows. You can retrieve these environment variables by using the % sign before and after the variable name.
Now that you know what environment variables are and how they work. Let's look at why you would want environment variables in the first place.
Why environment variables?
Since environment variables are associated across the whole system, you can access and use them across multiple programs and necessities in the system. This allows for numerous advantages like separation of data, easy management of environment-specific values, and better security.
You may have to work on projects in multiple environments. As in the local development environment, the production environment, or maybe the testing environment as well. Instead of having environment-specific values within code, it is preferable to use environment variables that are defined for the respective environment.
Environment variables also mitigate risks where one might have to include sensitive data within code. Data like passwords, tokens, and API keys should not be coupled within code that will be shared and even hosted in multiple places.
Environment variables are also used to store data essential for the functioning of the system. These variables affect how certain processes will behave. You may have also noticed some environment variables like PATH, TEMP or HOME. These variables hold values that have different purposes. For example, TEMP stores the location where processes may store files temporarily and HOME stores the location of the user's home directory.
As you know, processes have their own environment variables and they share them only with their child processes. This means environment variables can be used as a means of communication for data and preferences to child processes.
Conclusion
Environment variables are an integral part of the operating system. In a way, environment variables are variables for the operating system. Because of this, understanding environment variables takes you one step closer to understanding how the operating system functions and how you can use them to your advantage.