The nature/origin/passing of environmental variables

Question:

I am seeking assistance with understanding the intricacies of environmental variables as the information I’ve found through searching online is not detailed enough.

Point 1) What exactly are environmental variables? Specifically if they are predefined, inclusive, and built-in to many operating systems?

Point 2) What is the scope of operation for environmental variables? Do they only function when used with shells (Linux) or file explorers (Windows), or are they accessible through all programs and scripts such as Python or C++? Are they similar to standard input/output/error?

Point 3) Who is responsible for creating environmental variables? Are they set by the parent process for each program through methods such as fork() and exec(), or are they determined by the operating system?

Point 4) How are environmental variables passed around? Do child processes inherit the variables set by their parents, do changes made to the parent’s variables also apply to the child, or do child processes receive a new copy of the variables from the operating system?

Point 5) Are environmental variables a standardized concept that are consistent across the operating system, init, or some central authority, or are they created on a case-by-case basis without centralized or standardized heritage? For example, is there any relationship between PYTHONPATH and PATH?

Asked By: AlanSTACK

||

Answers:

Based on my research, this is my current understanding. Please let me know if any corrections are needed.

Answer 1) Environmental variables are a collection of strings that are passed from parent processes to child processes when forking.

Answer 2) Their scope is applicable to all processes, as it is a relationship between parent and child processes, unless the parent takes specific actions such as removing all inherited environmental variables.

Answer 3) Environmental variables are created by the parents, and the initial set of environmental variables is established by init, the parent of all processes.

Answer 4) The process of passing environmental variables occurs during the execution of the fork() and exec() functions by the parent process, it’s probably done by a method similar to set_env(…). But I have not researched it in depth.

Answer 5) I don’t have any historical context on this subject. I have no idea.

Answered By: AlanSTACK