- Understanding and viewing environment variables
- Persisting system-wide variables via
/etc/environment - Executing commands at login with
/etc/profile.d/
Understanding Environment Variables
Environment variables are key–value pairs that shells and applications use to adjust behavior. You can list the current environment with eitherenv or printenv.
| Command | Description | Example Output |
|---|---|---|
env | Displays all environment variables | PATH=/usr/local/bin:… |
printenv HOME | Shows the value of a specific variable (HOME in this case) | /home/aaron |
Setting System-Wide Environment Variables
While individual users often tweak~/.bashrc, you can enforce variables globally by editing /etc/environment:
KEY="value":
The file
/etc/environment only supports simple KEY="value" assignments. You cannot use shell expansions or commands here.Running Commands at Login
To execute scripts for every user at login, place them in/etc/profile.d/. For example, to record the last login timestamp:
/etc/profile.d/ are sourced by login shells automatically—you do not need a shebang (#!/bin/bash).
Avoid syntax errors in
/etc/profile.d/ scripts. A malformed script can prevent users from logging in properly.Further Reading & References
In the next article, we’ll explore advanced shell initialization techniques, including per-shell configuration and custom prompts.