Every process has an environment: a collection of name-value strings inherited from its parent process. Shells use environment variables to pass configuration such as language settings and executable search paths to the programs they start.
Text-Fu · Lesson 5
env (Environment)
Learn how Bash expands, exports, inspects, and temporarily overrides environment variables.
Expanding Variable Values in Bash
Bash expands $NAME or ${NAME} to a variable's value before it runs a command. Quote the expansion to preserve the value as one argument:
$ printf '%s\n' "$HOME"
/home/pete
Common environment variables include:
HOME: The current user's home-directory path.USER: A username supplied by the login environment on many systems.PWD: The shell's current working directory.PATH: Directories searched for command names.
Values depend on the current process environment; they are not universal constants. An unset variable expands to an empty string unless stricter shell behavior is enabled.
Which Bash command prints the value of HOME while preserving it as one argument?
Inspecting the Current Environment
Run env without operands to print the environment inherited by that env process:
$ env
The output contains NAME=value records, for example:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/bin
PWD=/home/user
USER=pete
Environment variables can contain credentials, tokens, internal paths, or other sensitive data. Do not paste complete env output into public issues or logs without reviewing and redacting it.
Which command prints the environment visible to a newly started process?
Finding Commands through PATH
PATH is a colon-separated list of directories that Bash searches when a command name contains no slash:
$ printf '%s\n' "$PATH"
The order matters: Bash uses the first suitable command it finds according to its resolution rules. Use type -a NAME to inspect how the current shell resolves a name.
To add /opt/coolapp/bin for the current shell and its future children while retaining the existing search path:
$ export PATH="/opt/coolapp/bin:$PATH"
Do not replace PATH accidentally with only the new directory, and do not add untrusted writable directories. Either mistake can prevent normal commands from resolving or cause an unexpected executable to run.
Which command adds /opt/coolapp/bin before the existing PATH for the current Bash process and its future children?
Exporting a Variable to Child Processes
Bash variables are not automatically part of the environment given to child processes. Mark a name for export with export:
$ export TEST=test
The current Bash process now has a variable named TEST, and commands it starts inherit TEST=test. A child process cannot use this mechanism to change its parent's environment.
$ printenv TEST
test
The assignment normally lasts until you unset it or the shell exits. It does not modify a system-wide environment.
What is the main effect of export TEST=test in Bash?
Setting a Value for One Command
Place assignments before a command to supply values only to that command's environment:
$ LANG=C sort names.txt
The current shell's LANG value is not permanently changed. The env utility provides another explicit form:
$ env LANG=C sort names.txt
Use env -i COMMAND to start a command with an initially empty environment, then add any required assignments. Many programs rely on environment values, so use that option deliberately.
Which command runs sort names.txt with LANG=C without permanently changing the current shell's LANG?
Loading Personal Values in Future Sessions
To recreate an exported variable in future interactive Bash sessions, place a suitable export line in the startup file those sessions actually read, commonly ~/.bashrc for interactive non-login Bash:
export TEST=test
Zsh commonly uses ~/.zshrc, while Fish uses different syntax and configuration. Login and non-interactive shells can read other files, so identify the shell and session type instead of assuming one file configures every process.
To practice environment inheritance and shell configuration, try these hands-on labs:
- Manage Shell Environment and Configuration in Linux - Practice creating and managing local and environment variables, understanding inheritance, and making configurations persistent by modifying the
.bashrcfile. - Environment Variables in Linux - Learn the concept and usage of environment variables, how to create, modify, and manage them, and their role in system configuration.
Lesson complete
You finished env (Environment)
You can now inspect and control the environment passed from Bash to child processes.
Expand variable values with deliberate quoting.
Review exported values without exposing secrets.
Preserve and order command directories in
PATH.Export a shell variable for future child processes.
Override a value for one command without changing the parent shell.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account