Get environment variables
👉 Overview
👀 What ?
Environment variables are dynamic-named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.
🧐 Why ?
Understanding environment variables is crucial because they constitute an integral part of the operating systems. They are used for making system decisions and for conveying information to various software applications. For developers and system administrators, knowing how to set, unset, and get environment variables is essential for scripting and automation.
⛏️ How ?
To get environment variables, one uses specific commands in the command line interface. In Linux, you can use the 'printenv' command followed by the name of the variable. In Windows, use the 'echo' command followed by the variable name prefixed with a percentage sign. It is important to note that the way to set, unset, and get environment variables may vary depending on the programming language and the operating system.
⏳ When ?
The use of environment variables in operating systems dates back to the early days of Unix in the 1970s. They have since become a standard feature in most modern operating systems.
⚙️ Technical Explanations
Environment variables are essentially stored in a key-value format within the operating system's shell. When a command is issued in the shell, the operating system retrieves the value associated with it. The flexibility of environment variables allows them to be set at different levels, including for the current shell instance, for user sessions, or even system-wide.
These variables are versatile and serve many functions. They can set the path for software libraries, which is crucial for running various software and scripts that depend on these libraries. They can also direct software behavior, providing a way for different software components to interact and function in a cohesive manner.
Another significant use of environment variables is storing data for use in shell scripting. Information like user profiles, file paths, and system settings can be stored in these variables, making them accessible across different scripts and shell sessions. This data storage function allows for more efficient and dynamic scripting, as scripts can pull the necessary data directly from these variables.
However, it's important to note that environment variables can also pose potential security risks if not handled properly. For instance, if a malicious actor gains access to your system, they could manipulate certain environment variables to execute harmful software. A common example is the PATH environment variable, which determines the directories that the shell searches for executables. If a malicious actor alters this variable, they can trick the system into executing malicious software instead of the intended software. Therefore, it's crucial to manage and monitor these variables carefully to ensure system security.
Here are detailed real-world examples of how to work with environment variables:
- Setting an Environment Variable:
In a Linux or macOS terminal, you can set an environment variable using the
export
command. For example, to set a variable namedMY_VARIABLE
with the valueHello
, you would use:
export MY_VARIABLE=Hello
In Windows, you would use the set
command:
set MY_VARIABLE=Hello
These commands set MY_VARIABLE
to Hello
for the duration of the current session.
- Getting an Environment Variable:
To retrieve the value of
MY_VARIABLE
in Linux or macOS, use theecho
command:
echo $MY_VARIABLE
In Windows, use the same echo
command but with percentage signs around the variable name:
echo %MY_VARIABLE%
These commands will output Hello
, the value we previously set for MY_VARIABLE
.
- Unsetting an Environment Variable:
To remove
MY_VARIABLE
from the environment in Linux or macOS, use theunset
command:
unset MY_VARIABLE
In Windows, use the set
command again, but this time without assigning a value:
set MY_VARIABLE=
These commands remove MY_VARIABLE
from the environment for the current session.
Remember, changes made to environment variables in this way will only last for the duration of the current session. To make permanent changes, you would need to add the export
or set
commands to a start-up script like ~/.bashrc
or ~/.bash_profile
on Linux or macOS, or the System Properties dialog on Windows.