Sub topic 4
Environment variables
This chapter assumes you can open a Windows command prompt and a Linux shell. It also assumes you know what a file path is and what a process is (a running program). Environment variables show up in the A+ Core 2 scripting objectives. They also explain many everyday support problems, such as "the command is not recognized".
What an environment variable is
An environment variable is a named value that the operating system keeps for running programs. A program or script reads the value by name instead of hard-coding it. A script that reads the user's home folder works for every user, because each user's session has a different value under the same name.
Every process gets its own copy of the variables when it starts. A child process, such as a script launched from a shell, inherits a copy from its parent. Changes made in the child do not flow back to the parent.
Common variables to know
Names differ between platforms. Windows names are not case-sensitive. Linux names are case-sensitive and are conventionally uppercase.
| Purpose | Windows (cmd) | Linux or macOS |
|---|---|---|
| Current user name | %USERNAME% | $USER |
| User's home folder | %USERPROFILE% | $HOME |
| Folders searched for commands | %PATH% | $PATH |
| Temporary files folder | %TEMP% | $TMPDIR (often unset, so /tmp is used) |
| Computer name | %COMPUTERNAME% | $HOSTNAME (a bash variable, not always exported) |
| Default shell | %COMSPEC% | $SHELL |
Displaying variables
In the Windows command prompt, wrap the name in percent signs. In Linux and macOS shells, put a dollar sign in front of the name. This is the form the exam expects:
# Windows: show the PATH environment variable
# echo %PATH%
# Linux/macOS: show the PATH environment variable
echo $PATHTo list every variable, use these commands:
- Windows command prompt:
setwith no arguments. - PowerShell:
Get-ChildItem Env:. A single variable is$env:PATH. - Linux:
printenvorenv.
PowerShell uses its own syntax. It treats environment variables as a drive named Env:, so you read them as $env:NAME:
# Read, list and set variables in PowerShell
Write-Output "User: $env:USERNAME"
Write-Output "Profile: $env:USERPROFILE"
Get-ChildItem Env: | Select-Object -First 5
$env:BACKUP_DIR = "D:\Backup"
Write-Output "Backups go to $env:BACKUP_DIR"Using them inside scripts
The same name-based lookup works in each script language. In a batch file, use the percent form:
@echo off
REM Print who is running the script and where their files live
echo User: %USERNAME%
echo Profile folder: %USERPROFILE%
echo Computer: %COMPUTERNAME%In a Linux shell script, use the dollar form. The export command marks a variable so that child processes inherit it:
#!/bin/bash
# Show inherited variables and pass a new one to a child process
echo "User: $USER"
echo "Home: $HOME"
export BACKUP_DIR="/mnt/backup"
bash -c 'echo "Child process sees: $BACKUP_DIR"'In Python, read the variables through os.environ. Use .get() with a default so a missing variable does not raise a KeyError:
import os
# USER on Linux/macOS, USERNAME on Windows
user = os.environ.get("USER") or os.environ.get("USERNAME", "unknown")
print("Running as", user)
print("PATH is", os.environ.get("PATH"))The earlier backup script in this tutorial uses $HOME and $USER in the same way. The paths adapt to whoever runs the script, so you do not have to edit it for each user.
Setting variables: scope and persistence
Setting a variable is easy. The common mistake is not knowing how long it lasts and who can see it.
| Command | Where | Lasts | Visible to |
|---|---|---|---|
| `set NAME=value` | cmd | Current window only | That window and its children |
| `setx NAME value` | cmd | Permanently, stored in the registry | New windows opened afterward, not the current one |
| `$env:NAME = "value"` | PowerShell | Current session only | That session and its children |
| `NAME=value` | bash | Current shell only | Only that shell |
| `export NAME=value` | bash | Current shell only | That shell and its children |
For a permanent Linux setting, add the export line to a startup file such as ~/.bashrc for one user. On Windows you can also use the graphical route: System Properties, Advanced tab, Environment Variables. That dialog separates user variables from system variables. User variables apply to one account. System variables apply to all accounts and need administrator rights to change.
The PATH variable
PATH holds a list of folders. Windows separates the entries with semicolons. Linux separates them with colons. When you type a command without a full path, the shell searches those folders in order and runs the first match.
This explains a typical support case. A technician installs a tool, then gets "'tool' is not recognized as an internal or external command" (Windows) or "command not found" (Linux). The tool's folder is not in PATH. The fix is to run the tool by its full path or to add its folder to PATH.
Order matters too. If two folders hold a program with the same name, the earlier folder wins. A malicious program placed in an early PATH folder can hijack a command name. For that reason, never add unfamiliar or world-writable folders to the front of PATH.
Common errors
- Reading a variable that is not set. In cmd,
%MISSING%is printed literally in an interactive window. Inside a batch file it expands to nothing. Either way, a script that builds a path from it can act on the wrong folder. - Expecting
setxto change the current window. It does not. Open a new window to see the new value. - Expecting a child to change its parent. A script that sets a variable does not change the shell that launched it. Run the script in the same shell if you need that, for example with
source script.shin bash. - Mixing up syntax.
$PATHin cmd and%PATH%in bash both fail to expand. - Skipping quotes in bash. A path containing spaces breaks unless you write
"$HOME/My Files". - Case mistakes on Linux.
$Pathand$PATHare different variables. - Storing secrets in variables. Any process running as that user can read them, and some tools write them to logs. Prefer a credential manager or a secrets vault for passwords and API keys.
Environment variables and remote work
Remote sessions get their own variables. If you connect to a Linux host over SSH, the shell starts with the remote user's $USER, $HOME and $PATH, not yours. A script that works when you run it locally can fail remotely if it assumes a variable that the remote session does not define. Print the variables you depend on at the start of the script when you are debugging.
Exercise
A technician runs setx REPORT_DIR "D:\Reports" in a command prompt and then immediately runs echo %REPORT_DIR% in the same window. Nothing useful is printed. Why, and what should the technician do?
Show answer
setx stores the variable permanently but does not update the current window's copy of the environment. Open a new command prompt to see the value, or use set REPORT_DIR=D:\Reports as well if the current window needs it immediately.
Exercise
A bash script sets MODE=test and then runs a child script that prints $MODE, but the child prints a blank line. What change makes the child see the value?
Show answer
Mark the variable for inheritance with export MODE=test (or write MODE=test ./child.sh to pass it for that one command). A plain assignment is visible only in the shell that made it.
Exercise
Which command displays the PATH variable in a Windows command prompt, and which displays it in a Linux shell?
Show answer
Windows: echo %PATH%. Linux: echo $PATH.