As defined by the POSIX (Portable Operating System Interface) standard, operating systems store a set of values called environment variables that processes can access for getting information about the system, or configuration values for instance. Each process can set new environment variables or modify current ones, and pass them to child processes.
Each environment variable has a name. They are thus stored as name/value pairs, and retrieve through their names.
Here is an example in Bash for setting an environment variable:
export MY_VAR=some_valueStill in Bash, we retrieve the value of the environment variable as any other Bash variable:
echo "$MY_VAR"some_value
Each programming language proposes functions to retrieve and set environment variables.
The following Python program prints the same environment variable:
bat print_env_var.pyimport os
print(os.environ['MY_VAR'])
Here is its effect:
python print_env_var.pysome_value
We can do the same in Rust:
bat print_env_var.rsfn main() {
if let Ok(s) = std::env::var("MY_VAR") {
println!("{s}");
}
}
The effect is the same:
./print_env_varsome_value