stackademic

The leading education platform for anyone with an interest in software development.

Environment Variables

Store and use configuration values in your shell environment

Overview

Environment variables are named values that the shell and the programs it launches can read. They configure behavior — like where to find executables (PATH) or which editor to use (EDITOR) — without hardcoding values into programs. Understanding them is key to configuring tools and writing portable scripts.

Syntax / Usage

Set variables, export them to child processes, and read them with $.

NAME="Stackademic"        # a shell variable (this session only)
echo "$NAME"              # read the value

export API_URL="https://api.example.com"  # export to child processes
env                       # list all environment variables
echo "$PATH"              # show the executable search path
printenv HOME             # print a single variable

unset API_URL             # remove a variable

Examples

Set a variable for a single command without exporting it globally:

NODE_ENV=production node server.js

Add a directory to your PATH so its programs are runnable:

export PATH="$HOME/bin:$PATH"

Read a configuration value inside a script:

echo "Deploying to $API_URL"

Common Mistakes

  • Forgetting export, so child processes can't see the variable
  • Putting spaces around =, e.g. NAME = value, which the shell rejects
  • Not quoting "$VAR", causing word-splitting when the value has spaces
  • Overwriting PATH instead of appending, which breaks common commands
  • Expecting variables set in one terminal session to persist to another

See Also

command-line-navigation command-line-shell-scripting