Sub topic 4
Environment variables
This chapter assumes you can open a Windows command prompt and a Linux shell, read file paths, and know what an IP address and port are. It builds on the previous chapter, where a variable was a name a script uses to hold a value.
What an environment variable is
An environment variable is a named value that the operating system keeps and hands to every program it starts. A script does not create it. The script reads it. Because the value comes from the system, the same script can run on many computers and still adapt to each one.
Common examples:
| Purpose | Windows | Linux/macOS |
|---|---|---|
| Folders searched for commands | `PATH` | `PATH` |
| Current user's name | `USERNAME` | `USER` |
| Current user's home folder | `USERPROFILE` | `HOME` |
| Temporary files folder | `TEMP` | `TMPDIR` (often unset; `/tmp` is the usual default) |
PATH matters most. When you type a command name, the system looks through each folder listed in PATH until it finds a matching program. If a tool runs only when you type its full path, its folder is probably missing from PATH.
Displaying environment variables
Windows wraps the name in percent signs. Linux and macOS put a dollar sign in front of it.
@echo off
REM Show two variables, then list all of them
echo %PATH%
echo %USERNAME%
set#!/bin/bash
# Show two variables, then list all of them
echo $PATH
echo $USER
printenvIn PowerShell, use the Env: drive: $env:PATH shows one variable, and Get-ChildItem Env: lists all of them.
Using them in scripts
A script that reads a variable instead of hard-coding a value works for any user. The Python script below reads the same variables on any platform.
import os
user = os.environ.get("USER") or os.environ.get("USERNAME")
home = os.environ.get("HOME") or os.environ.get("USERPROFILE")
print(f"Backing up files for {user} from {home}")os.environ.get returns None if the variable does not exist, so the script does not crash. Windows sets USERNAME, and Linux sets USER, so the script checks both.
Setting variables
A variable you set inside a session lasts only for that session and its child processes.
- Windows:
set MYVAR=helloaffects only the current command prompt window.setx MYVAR hellosaves it for future sessions but not the current one. - Linux:
export MYVAR=hellomakes the value visible to programs started from that shell. Add the line to~/.bashrcto keep it.
Common errors
- Writing
$PATHin a Windows batch file, or%PATH%in bash. The wrong syntax prints literal text. - Expecting
setxto change the window that is already open. - Overwriting
PATHinstead of appending to it, which breaks most commands.
Exercise
A technician runs set BACKUPDIR=D:\Backup in one command prompt window, then opens a second window and runs echo %BACKUPDIR%. What prints, and which command would make the value available in new windows?
Show answer
The second window prints the literal text %BACKUPDIR% because set only affects the window where it was run. Running setx BACKUPDIR D:\Backup saves it for windows opened afterward.