Skip to content
DevNursery.com - New Web Developer Docs
GitHub

Environment Variables

Environmental Variables

Environment variables are a fundamental concept in computer science and software development. They are essentially dynamic values that can affect the behavior of software and operating systems. Environment variables store configuration settings, system paths, and other essential information that programs and scripts can access.

Here are some key points about environment variables:

What They Are:

Environment variables are key-value pairs that store information at the system level. They are often used to configure software, define system-wide settings, or provide data that programs need to function correctly. These variables are accessible to all processes running on a system.

How to Use Them in Bash (Linux/macOS):

To view the value of an environment variable, use the echo command followed by the variable name preceded by a dollar sign (e.g., echo $HOME).

To set or modify an environment variable for the current session, use the export command (e.g., export MY_VARIABLE="some value").

To make an environment variable persist across sessions, you can add the export command to your shell profile file (e.g., .bashrc or .bash_profile).

How to Use Them in PowerShell (Windows):

To view the value of an environment variable, use the echo command with the variable enclosed in percentage signs (e.g., echo %USERPROFILE%).

To set or modify an environment variable for the current session, use the $env: prefix (e.g., $env:MY_VARIABLE = "some value").

To make an environment variable persist across sessions, you can use the “System Properties” settings in the Control Panel or modify the Windows Registry.

.env Files:

A .env file is a plain text configuration file used to store environment variables. These files are often used in web development projects to define variables like database connection strings, API keys, and other sensitive information.

Each line in a .env file typically consists of a variable name and its associated value (e.g., DB_USERNAME=johndoe).

Tools like dotenv in JavaScript and libraries in other languages can load variables from .env files into the environment.

Environment variables are versatile and widely used in various contexts, from system configuration to application development. They provide a way to separate configuration from code, making it easier to manage settings and secrets across different environments (e.g., development, testing, production) without hardcoding them into your scripts or applications.

Env Vars across languages

LanguageAccessing Env VarsLibrary for .env Files
Pythonos.environ.get('VAR_NAME')python-dotenv
JavaScriptprocess.env.VAR_NAMEdotenv
RubyENV['VAR_NAME']dotenv
PHPgetenv('VAR_NAME')vlucas/phpdotenv
Goos.Getenv("VAR_NAME")github.com/joho/godotenv
JavaSystem.getenv("VAR_NAME")N/A
C# / .NETEnvironment.GetEnvironmentVariable("VAR_NAME")N/A
SwiftProcessInfo.processInfo.environment["VAR_NAME"]N/A
Ruststd::env::var("VAR_NAME")N/A
Perl$ENV{'VAR_NAME'}N/A
Bash$VAR_NAMEN/A
PowerShell$env:VAR_NAMEN/A
Luaos.getenv("VAR_NAME")N/A
KotlinSystem.getenv("VAR_NAME")N/A
TypeScriptprocess.env.VAR_NAMEdotenv

Commonly Used Environmental Variables

Here is a list of commonly used variables and whether they are typically defined by the operating system or the developer.

Variable NameDescriptionProvided By
PATHSpecifies the directories where executable programs are locatedOS
HOMESpecifies the user’s home directoryOS
USERSpecifies the username of the current userOS
LOGNAMESpecifies the login name of the current userOS
TEMP or TMPDIRSpecifies a temporary directory for storing temporary filesOS
PWDSpecifies the current working directoryOS
LANG or LANGUAGESpecifies the current language and locale settingsOS
LD_LIBRARY_PATHSpecifies the directories where shared libraries are locatedOS/Programmer
JAVA_HOMESpecifies the location of the installed Java SDKProgrammer
PYTHONPATHSpecifies additional directories to search for Python modulesProgrammer
CLASSPATHSpecifies the location of Java class filesProgrammer
DB_URLSpecifies the URL of a database serverProgrammer
API_KEYSpecifies an API key for accessing external servicesProgrammer
SHELLSpecifies the path to the user’s preferred shellOS
EDITORSpecifies the user’s preferred text editorOS/Programmer
MAILSpecifies the location of the user’s mailboxOS
DISPLAYSpecifies the X Window System display serverOS
TZSpecifies the timezone of the systemOS
TERMSpecifies the type of terminalOS
HTTP_PROXYSpecifies the HTTP proxy serverProgrammer
HTTPS_PROXYSpecifies the HTTPS proxy serverProgrammer
NO_PROXYSpecifies the domains that should bypass the proxy serverProgrammer
HOSTNAMESpecifies the hostname of the systemOS
PORTSpecifies the port number for a network serviceProgrammer
DEBUGSpecifies whether to run the application in debug modeProgrammer
CONFIG_PATHSpecifies the path to a configuration fileProgrammer
NODE_ENVSpecifies the environment in which a Node.js app is runningProgrammer
DATABASE_URLSpecifies the URL of a database serverProgrammer
SECRET_KEYSpecifies a secret key for encrypting dataProgrammer
AWS_ACCESS_KEY_IDSpecifies the AWS access key IDProgrammer
AWS_SECRET_ACCESS_KEYSpecifies the AWS secret access keyProgrammer
REDIS_URLSpecifies the URL of a Redis serverProgrammer
RACK_ENVSpecifies the environment in which a Ruby app is runningProgrammer
MONGO_URLSpecifies the URL of a MongoDB serverProgrammer
GOPATHSpecifies the workspace directory for Go projectsProgrammer
ANDROID_HOMESpecifies the location of the Android SDKProgrammer
SDKROOTSpecifies the root directory of the SDK being usedProgrammer
PGDATASpecifies the location of the PostgreSQL data directoryProgrammer
NPM_CONFIG_PREFIXPrefix configuration where npm will find/keep packagesProgrammer
NVM_DIRNVM (Node Version Manager) directory pathProgrammer
RBENV_ROOTDirectory path for rbenv (Ruby Version Manager)Programmer
RUBYOPTSpecifies additional options to Ruby interpreterProgrammer
BUNDLE_PATHPath where Bundler installs your gemsProgrammer
RAILS_ENVSpecifies the environment in which Rails app is runningProgrammer
GEM_HOMEThe directory where gems are installedProgrammer
GEM_PATHPath where RubyGems looks for gemsProgrammer
RUSTUP_HOMEDirectory path for rustup (Rust toolchain installer)Programmer
CARGO_HOMEDirectory path for Cargo, Rust’s package managerProgrammer
LOCALAPPDATAPath to the local app data directory on WindowsOS
APPDATAPath to the roaming app data directory on WindowsOS
CommonProgramFilesPath to the common program files directory on WindowsOS
ProgramFilesPath to the program files directory on WindowsOS
SystemRootPath to the Windows system root directoryOS
GOPROXYURL of a Go module proxyProgrammer
GRADLE_HOMESpecifies the location of the installed GradleProgrammer
KUBECONFIGPath to a kubeconfig file for access to KubernetesProgrammer
DOCKER_HOSTSpecifies the hostname or IP address of the Docker daemonProgrammer
DOCKER_CERT_PATHPath to directory with Docker TLS certificatesProgrammer
DOCKER_TLS_VERIFYEnable or Disable TLS verification for DockerProgrammer
COMPOSE_PROJECT_NAMESpecifies a project name for Docker ComposeProgrammer
VIRTUAL_ENVSpecifies the path to a Python virtual environmentProgrammer
FLASK_ENVSpecifies the environment in which Flask app is runningProgrammer
FLASK_APPSpecifies the name of the Flask applicationProgrammer
COMPOSER_HOMEDirectory path for Composer, the PHP dependency managerProgrammer
COMPOSER_VENDOR_DIRDirectory path for Composer’s vendor directoryProgrammer
ANT_HOMESpecifies the location of the installed Apache AntProgrammer
MAVEN_HOMESpecifies the location of the installed Apache MavenProgrammer
TOMCAT_HOMESpecifies the location of the installed Apache TomcatProgrammer
SPRING_PROFILES_ACTIVESpecifies active profiles for a Spring applicationProgrammer
KAFKA_HOMESpecifies the location of the installed Apache KafkaProgrammer
ZOOKEEPER_HOMESpecifies the location of the installed Apache ZooKeeperProgrammer
HADOOP_HOMESpecifies the location of the installed Apache HadoopProgrammer
HADOOP_CONF_DIRSpecifies the directory of Hadoop configuration filesProgrammer
SPARK_HOMESpecifies the location of the installed Apache SparkProgrammer
PYSPARK_PYTHONSpecifies the Python binary executable for PySparkProgrammer
PYSPARK_DRIVER_PYTHONSpecifies the Python binary executable for the PySpark driverProgrammer
CASSANDRA_HOMESpecifies the location of the installed Apache CassandraProgrammer
MYSQL_HOMESpecifies the location of the installed MySQL serverProgrammer
PGHOSTSpecifies the host name of the PostgreSQL serverProgrammer
PGPORTSpecifies the port number of the PostgreSQL serverProgrammer
PGUSERSpecifies the username for the PostgreSQL serverProgrammer
PGPASSWORDSpecifies the password for the PostgreSQL serverProgrammer
ANDROID_SDK_ROOTSpecifies the location of the Android SDKProgrammer
GOBINSpecifies the directory where Go binaries are installedProgrammer
JRE_HOMESpecifies the location of the installed Java Runtime EnvironmentProgrammer
JDK_HOMESpecifies the location of the installed Java Development KitProgrammer
SCALA_HOMESpecifies the location of the installed ScalaProgrammer
PERL5LIBSpecifies additional directories to search for Perl librariesProgrammer
R_LIBSSpecifies additional directories to search for R librariesProgrammer