You don’t have to be a programmer to have to deal with environment variables in some case. It may be, for example, that when installing or configuring a Windows program you have come across a path that, instead of following the typical structure “C: / Users / YoMismo / folder” shows something like “% USERPROFILE% /binder”.
That’s because% USERPROFILE% is a environment variable: that is, a text string that operating systems like Windows, Linux or Mac use to store values that they can vary from one computer to another – or, as in this case, from one user to another – but that, nevertheless, need a unified way of accessing it.
Explain it to me more slowly
Normally, these values refer to files, directories and common system functions whose specific path may vary, but that other programs need to be able to know.
The example above allows a program know how to access your user folder even if you have not indicated its name. Or even if it doesn’t know which version of Windows you are using (let’s remember that the ‘C: Users ‘ of Windows 10 was ‘C: Document and Settings ‘ in Windows XP).
It does not matter, because all that information is defined in the environment variables, guaranteeing that all Windows programs can carry out their work correctly on any computer. Because there are many more environment variables besides % USERPROFILE%. Let’s see some:
% APPDATA% – It refers to the hidden folder for program data, inside the user box. In Windows 10 the default path is * “C: Users Username AppData Roaming”.
% COMMONPROGRAMFILES% – It refers to the folder where the programs store common files. In Windows 10 the default path is “C: Program Files Common Files”.
% PROGRAMFILES% – It refers to the folder where the programs are installed. In Windows 10 the default path is “C: Program Files”.
% WINDIR% – It refers to the folder where Windows is installed. In Windows 10 the default path is “C: Windows”.
But environment variables don’t always equal directory paths: they can refer to other kinds of information. So, % TIME% returns the current system time, %YOU% the version of the operating system and % PATHEXT% the list of file extensions considered executable (the list usually includes, in addition to the .EXE files such as .BAT, .COM, .CMD, .JS., .JSE, .MSC, .VBE,. VBS, .WSF, .WSH, etc).
But perhaps the environment variable that we will most often have to deal with will be % PATH%. And what is its function? Have you noticed that when you type your own Windows command (for example, Regedit) no need to type the full path of the executable?
That is because, every time we type a command, the system checks the folders contained in the variable% PATH% to check if any executable file matches the same.
It is a widely used resource, for example, by developers who want to call an interpreter or compiler from the project folder in which they are working; many IDEs also turn to% PATH% to run such tools.
Here is what happens when we call the Java interpreter but we have not included the JRE directory in the Windows% PATH%.
Check and edit our environment variables in Windows
If you want to check if the values of these variables on your computer match those shown here, you can open a command line window (CMD, not the Power Shell) and type “ECHO” followed by the variable in question. If you prefer to list all the variables and their respective values, go (now yes) to the Power Shell and type “Get-ChildItem Env:”.
But if you don’t like going to the command line, there is another tool that will allow us not only to check the value of each variable, but also to edit them very easily. We just have to enter ‘Advanced system settings’ in ‘Search’, and open “Environment variables” in the window that appears. And something similar to this will appear:
Environment variables in Windows 10
Once we reach this point, we only have to select the variable that we are interested in changing and click “Avoid”. We can also add new variables or remove them.
Command variables in Linux
In the case of Linux, the role of environment variables is the same as in Windows, although we will not find exactly the same or with the same names.
In this operating system, we must use the ‘printenv’ command to display both the complete list of variables as the value of each of them individually.
Contents of a ‘/ etc / environment’ file, viewed with the Nano text editor.
Thus, in Linux we find variables such as ‘SHELL’ (shell that will interpret the commands, in most distributions it will be Bash), ‘LANG’ (current language) or ‘HOME’ (current user’s home directory).
To change its values, we will have to resort to editing, mainly, three text files:
“/ etc / environment” – For variables independent of the command interpreter.
“etc / profile” – The variables that we define here will be valid for all interactive shells that require login. Its equivalent if we want to define only user variables is ~ / .bash_profile.
“/etc/bash.bashrc” – Same as above, but for non-login shells. Its equivalent if we want to define only user variables is ~ / .bashrc.