Contents:

Python Basics: A Free Course for All Levels ➞ Developing 4 Impressive Projects and Interacting with experts. Find out what skills you can master!
Learn MoreThe other day, I was preparing content for a virtual lesson that focused on environment variables in the Linux operating system. We're talking about variables like PATH, PS1, and TERM, which are familiar to anyone who has ever used the command line.
Sometimes students ask me, "Does anyone really need this these days?" And I can confidently say that yes, they do! Environment variables are a fundamental tool for configuring applications on any Unix-based system. This applies to both macOS laptops and cloud servers or Kubernetes containers.
The twelve-factor app, an outstanding manifesto for building scalable web applications, including microservices, clearly and unambiguously states the idea in the section on configurations: it is necessary to isolate configurations from code and store settings in environment variables.

Technical Director at Tabby, a fintech company, and the creator of Python testing courses at Skillbox.
In 2000, I first encountered a black FreeBSD 4.0 screen, and from that moment on, I fell in love with the Unix console at first sight.
Definition and essence of environment variables
Environment variables are a collection of values that regulate the parameters and functionality of the operating system, as well as programs running within it. These variables are organized as key-value pairs and stored in RAM, which makes interaction with applications more convenient. However, most publications on this topic limit themselves to these definitions, without delving into the details of their implementation.
It is important to remember that our perception of the functioning of any technology is a mental model that needs to be adjusted and refined. This can be done most effectively by looking back at the initial stages of its development, when the concept was just beginning to take shape and had not yet been saturated with various improvements. Let's analyze when environment variables appeared and how they have evolved to this day.
Environment variables, as we know them today, began their existence in 1979 with the seventh version of the Unix operating system (Unix V7). If you analyze the lineage of Unix-like operating systems, you will notice that this system occupies a fundamental place in history, marking the beginning of the evolution of such operating systems as Linux, macOS, and FreeBSD. One of the computers on which this version of Unix ran was the PDP-11.

It has been a long time since the development of environment variables. More than forty years! What were the authors' goals when they created the concept of environment variables and the environment itself? How relevant is it today? How exactly was this solution implemented? If you've gotten this far and you're curious about the answers to these questions, I suggest we explore them together!
The Essence and Significance of Environment Variables
First, let's analyze the concept of configuration and try to understand its origins. John, a fictional DEC lab worker, will help us with this. Hi, John!
DEC is a company known for its computer developments, particularly the PDP-11 and other technologies. John had a Teletype Model 33 terminal, which can be seen in the image above. It was easy and convenient to use: John simply typed the command cat into the terminal to launch the executable file. The PDP-11 printed the application's output at 100 words per minute, printing line by line, reminiscent of a typewriter. Although it was quite noisy, the process was fast.
Everything worked flawlessly, but time moves on. Soon, DEC introduced the VT52 video terminal, and one of them ended up in John's hands.

The new terminal differs from its predecessor - instead of paper, it is equipped with a screen! This resulted in a change in the number of characters displayed: the Teletype Model 33 could display 72 characters per line, while the VT52 supported 80 characters. These parameters cannot be changed: the Teletype Model 33 functions as a typewriter, and the VT52 can only display ASCII characters and only in one size.
However, the version of cat that John was using only allowed for 72 characters per line. New versions of the applications would need to be developed that would support the VT52. So, it turns out that different terminals would require two versions of cat, differing only in one minor setting? This is inefficient, especially considering that the number of device models may continue to grow. But cat has no way of determining which terminal John is currently working with.
The most obvious way to convey this information to him is to specify a parameter corresponding to the terminal being used when the application is started. For example, you could enter cat TERM=dumb for a Teletype Model 33 terminal or cat TERM=vt52 for modern monitors. John discussed this issue with Dennis Ritchie, and a new version of cat was soon released that supported lines of any length. Now this command can be used on both old and new devices, simply by specifying the appropriate parameter.
Separating the configuration into application parameters and the environment was an important step that allowed us to more flexibly manage settings. We had already realized that parameterizing the application allows us to use a single file in various situations. However, the question arises: why did we decide to separate the configuration for the application and for the environment?
By that time, many applications had already been developed on the Unix system. Each of them needed to implement support for the TERM variable, which indicated the specific environment with which they interacted. However, manually specifying TERM in the parameters is redundant. The optimal solution would be to configure the environment once, allowing Unix to automatically supply the appropriate parameters for all applications launched. It was this mechanism, which involves separating parameters into the environment and application-specific settings, that was implemented in Unix V7.
Moving some parameters into the environment avoids the need for individual configuration for each application.
The concept is clear, thanks John!
Going to the Source: The Environment Manual
What did environment configuration look like in early versions of Unix? To answer this question, let's turn to The UNIX Time Sharing System — UNIX Programmer's Manual — Seventh Edition, Volume 1A, published in January 1979. The section on the Shell, on page 161, contains information about the environment.
Let me summarize the contents:
- The environment is a set of "key" and "value" pairs that is passed to a program during its execution, similar to a standard set of arguments.
- To enter environment variables, use the export command.
- Commands executed from the shell share a common environment.
- The shell forms parameters based on environment variables.
Everything functions similarly to how it works in modern Unix-like operating systems. The documentation also mentions environment variables such as PATH, HOME, TERM, and others familiar to us from modern programming.
From a user's perspective, the approach to managing environment variables was developed at the very beginning of Unix's existence and has remained unchanged since then.
At this point, I realized that this exercise would certainly not be included in the online lesson materials, but I couldn't stop my "investigation." I wanted to find out where exactly this environment was stored on the Unix system.
The Space Under the Hood of a Car
To understand what was implemented in Unix for the environment to function, I turn again to the documentation. The links provided in the section on Shell direct us to ENVIRON (5) - the section number in the documentation is indicated by a number in parentheses.
The documentation is presented briefly. Environ is a global variable that is formed when the EXEC (2) command is executed. Because it is a global variable, information about the environment is stored in the process's RAM. In the context of the operating system, a process is considered to be an active executable file.
What is the EXEC call and how does it form Environ in application memory?
The EXEC call is made while the user is running in the Shell. When the user decides to run a program, he enters the path to its executable file and leaves further actions to the Shell. However, the Shell doesn't initiate the application launch itself either—it transfers control to the EXEC function, providing it with the parameters necessary to launch the program, as well as settings related to process management in the Unix operating system.
How exactly does Unix handle EXEC processing? Let's turn to the documentation. When a C program is launched, the operating system initiates the main function, passing it the main arguments (argc, argv, envp). The argc value indicates the number of arguments passed, and argv is an array of pointers to these arguments. As for envp, it is a pointer that refers to the environment of the parent process. In this case, this will be the Shell with its variables.
However, before the main process, main, is launched, a copy of the environment represented by the envp variable is created. This copy will be available to the application through the global variable environ. The shell generates the list of variables, while the operating system itself creates a copy of it in the new process's memory.
Thus, the new process receives a duplicate version of its parent's environment. This copy allows each application to change its settings without fear of damaging the functioning of other applications. If the new process decides to launch another application, a child process will be created that inherits a copy of the environment only from its parent, without regard for previous processes.
Unix calls provided support for working with environment variables, with each application receiving its own individual copy of the parent process's environment, stored in its memory.
The concept of an environment was first introduced in the seventh version of Unix, as evidenced by the absence of mention of it in the sixth version manuals. The documentation for the EXEC (2) system call only lists two parameters, argc and argv, while the envp parameter is not mentioned, indicating that the concept of an environment did not yet exist at that time.
In addition to the V7 manual - Annotated Excerpts from the Programmer's Handbook - you can find a mention that the Shell (Bourne shell) we know was developed specifically for the seventh version, replacing the previous one. Without this shell, working in the environment would be significantly less comfortable.
How has the environment changed since then?
So, we have all the necessary answers, but I am interested to know how things stand with the environment in modern versions of Linux. Perhaps there have been some changes in the way it is managed?
I am testing on a Linux system running in a Docker container, and I am using standard developer tools to accomplish this. In particular, I am using the Strace utility to monitor system calls and GDB to analyze the process memory contents.
Before I dive into the discussion of calls, I would like to first outline what exactly I am going to consider. As a starting point, I will choose the exec() call. Why this call? The reason is that POSIX standards have existed since 1988, which regulate the structure of system calls.
Unix V7, which I was talking about, was released earlier, but the POSIX standards were largely influenced by Unix-like systems. Therefore, Linux largely complies with the POSIX standards, which allows us to look for similar calls in this operating system.
This time, I do not need to consult online documentation. I can use the man command, which provides information on how to use other commands.
In an Ubuntu container, we run the man exec command to access the manual. In the Linux operating system, exec is a broad family of system calls, which also includes the global environ variable and the ability to pass the environment using the envp pointer. However, to trace the required call using strace, you need to know its name, and according to man exec, it is the execve call. Thanks to POSIX standards, the environment management mechanisms in Linux are significantly similar to those implemented in Unix V7.
Let's run the cat command in the Bash shell to get acquainted with its invocation parameters.
The third argument is an array of variables pointed to by the pointer 0xaaaadacacb50. I wonder what is stored in this array? To get information, rerun strace with the -v option.
So, as the third argument, we again encounter familiar environment variables such as PID, HOSTNAME, HOME, and so on. In essence, this process is not much different from using the exec() function in the seventh version of Unix. The only thing that has changed is the need to count arguments.
Let's continue our exploration. The invocation information does not contain any indication of where to find the environment variables in the memory of the active process. However, it is curious, because this is where the environment we so often mention is located.
I have information - the pointer address from strace is 0xaaaadacacb50. Let's check what is located at this address in bash memory:
Wow, this is the very marker that signals the start of the bash process environment. We can examine the addresses of these markers and find that they store our environment variables:
Looks like it's time to take a break.
Instead of conclusions
I started my career working in the console, and environment variables have become a familiar and simple tool for me. I was amazed that their use has remained unchanged for so long.
Although early Unix releases have fallen into disuse, they formed the basis for modern operating systems like macOS and Linux, which are found on most computers. Understanding this legacy requires interest, access to old manuals, and knowing where to look for information.
I would like to thank those who work to preserve and publish documentation for legacy versions of programs. Without their efforts, I would not have been able to conduct this short research.
For more fascinating facts about coding, join our Telegram channel. We'll be glad to see you among our subscribers!
Read also:
- Key commands and keyboard shortcuts for working in the Linux terminal, as well as in Unix, macOS, and FreeBSD systems.
- Test: How much do you understand about processor architecture?
- Using Bash to speed up tasks on your computer can significantly increase your productivity. There are several methods that will help you achieve this goal.
First, mastering terminal commands is a fundamental step. Knowing basic commands such as ls, cd, cp, mv, and rm will allow you to quickly navigate between directories, copy, and delete files without having to use graphical interfaces.
Second, using Bash scripts can greatly simplify and automate routine tasks. Creating a script that executes a sequence of commands will save time and reduce the likelihood of errors.
The third important aspect is the use of aliases. Aliases allow you to create short and convenient commands for frequently used commands, which speeds up the process of completing tasks.
Also, it is worth paying attention to the use of pipes and redirection. This will allow you to combine several commands, passing the result of one command as input to another, which makes the work more efficient.
Don't forget about the use of variables and functions. Variables will help you store temporary data, and functions will allow you to organize your code and reuse it if necessary.
In addition, you can study various tools and utilities, such as `grep`, `awk`, `sed`, which significantly expand the functionality of Bash and make processing text data more convenient and faster.
Finally, you should not neglect the opportunity to use package managers to install new utilities that can complement your work in Bash and improve overall productivity.
By following these guidelines, you can use Bash to perform tasks on your computer more efficiently.

