env variables
An environment variable is a key-value pair of data that is managed or accessed by the operating system, programs, or scripts. When a process is created, it will receive a copy of environment variables either from the operating system or its parent process, and any additional ones provided to it. The key is a string identifier that is case sensitive in Linux and macOS, but not Windows. Naming conventions for the key include PascalCase, SCREAMING_SNAKE_CASE, and snake_case. The value of an environment variable is also a string and in some cases it may need to be wrapped in quotes when defining a key-value pair. Sometimes data are alternatively passed as command-line arguments. Some data are server-side only and not intended for access on end-user devices. Also, if the system has non-root users, they might still be able to see a process' environment variables and alternatives should be considered [1, bullet point six].
Example use cases
- Settings
- app environment: production or development
- Paths to folders, files, and programs
- Networking
- Ports
- API Endpoints
- Security
- Encryption Keys
- Credentials
- Username and Password (superuser, database accounts)
- API Keys
Table: Files and Folders
/etc/environment/ | A file for defining system-wide environment variables on Unix and Linux machines. |
Shell Configuration Files | These files can be configured with environment variables to be added, such as when the user logs in or when a non-login shell is created. |
.env file | A plain-text file parsed by programs to add environment variables during initialization and is typically omitted from version control management systems. A .env template is used to create a .env file and consists of keys with empty values or predefined values. |
Shells
Windows Command Prompt
Windows PowerShell
Unix Shell
// shell variable (not technially an 'environment variable')
IDENTIFIER=VALUE
// shell environment variable
export IDENTIFIER=VALUE
Windows
Registry
Computer\HKEY_CURRENT_USER\Environment
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
GUI Editor
Search > Edit Environment Variables for Your Account
Search > Edit the System Environment Variables
System > Settings > About > Advanced System Settings > Advanced > Environment Variables
Linux
Table: Linux tools
name | path |
---|---|
env | /etc/bin/env |
printenv | /etc/bin/printenv |
macOS
Table: macOS tools
name | path |
---|---|
env | /usr/bin/env |
printenv | /usr/bin/printenv |
Resources
- https://www.ibm.com/support/pages/process-environment-and-arguments-argenv
- https://www.debian.org/doc/manuals/debian-handbook/sect.shell-environment.en.html
- https://learn.microsoft.com/en-us/windows/win32/procthread/environment-variables
References
[1] The PostgreSQL Global Development Group. "34.15 Environment Variables." PostgreSQL Documentation. [Online]. Available: https://www.postgresql.org/docs/current/libpq-envars.html. (accessed: Aug. 04, 2024).