Environment variables are user-definable values that can affect the way running processes behave on a computer. They are part of the environment in which a process runs and can be accessed through the shell in Linux. Environment variables are typically named in uppercase, with words joined with an underscore, and are case-insensitive in Windows. They are used to store dynamic values that can come from a range of sources, such as text files, third-party secret managers, and calling scripts. The value of these variables is not hardcoded in the program and can be changed based on the environment that the program is running in.
There are two types of environment variables:
-
System Environment Variables: These variables are available to all users on a system and are used to store information that is common to all users, such as the path to a global package/library that can be used by all users in the system.
-
User Environment Variables: These variables are local to a user profile in Windows systems and are used to store user-specific information, such as the path to a local installation of libraries that are not to be used by all users, values specific to programs installed only for specific users, etc. .
Environment variables are useful for adhering to the separation of concern design principle, which states that computer programs should be divided into distinct sections to manage them efficiently. They provide a good way to affect and change the way applications run and are commonly used to set up different configuration options for different environments.
Some commonly used environment variables in Linux include:
$USER
: Gives the search path for commands.$PATH
: Gives the search path for commands.$HOME
: Gives the path of the home directory.$PWD
: Gives the path of the present working directory.$HOSTNAME
: Gives the name of the host.$LANG
: Gives the default system language.$EDITOR
: Gives the default file editor.$UID
: Gives the user ID of the current user.$SHELL
: Gives the default shell.
Environment variables are helpful because they allow you to change which of your environments use which third-party service environment by changing an API key, endpoint, or whatever the service uses to distinguish between environments.