π± Linux/Unix: Environment Variables (env, printenv) β Manage and View System Environments
π§² Introduction β Why Learn About Environment Variables?
Environment variables in Linux/Unix are key-value pairs that define the system’s operational context. They affect shell behavior, user sessions, script execution, and software configurations. Commands like env and printenv help you explore and manipulate these variables efficiently.
π― In this guide, youβll learn:
- What environment variables are and how theyβre structured
- How to view them using envandprintenv
- How to set, export, and use them in scripts
- Real-life examples of commonly used variables
π What Are Environment Variables?
Environment variables:
- Store temporary data for programs, shells, and users.
- Influence login behavior, path searches, editors, etc.
- Are inherited by child processes of a shell or script.
π§ͺ Examples:
echo $HOME
echo $USER
echo $PATH
π§° env β Display or Run Commands in Modified Environments
β Syntax:
env [options] [command]
π Description:
- Lists all current environment variables.
- Can be used to temporarily set variables for a specific command.
π§ͺ Examples:
env                     # List all environment variables
env | grep PATH         # Filter specific variable
env MYVAR=value command # Run command with temporary variable
π‘ Example:
env EDITOR=vim nano     # Temporarily set EDITOR to vim for this session
π€ printenv β Print Environment Variables
β Syntax:
printenv [variable_name]
π Description:
Displays the value of one or more environment variables.
π§ͺ Examples:
printenv           # Show all environment variables
printenv HOME      # Show value of HOME variable
printenv PATH USER # Show multiple variables
π§ͺ Setting Environment Variables
πΈ Temporary (Current Shell Only):
MYVAR="Hello"
echo $MYVAR
πΈ Export to Subprocesses:
export MYVAR="Linux"
πΈ Temporary Command-Level:
MYVAR="Now" echo $MYVAR
π§ These vanish when the session ends.
π§ Persistent Environment Variables
To set variables permanently, add them to shell config files:
| File | Purpose | 
|---|---|
| ~/.bashrc | User shell session (interactive) | 
| ~/.bash_profile | User login shell (login) | 
| /etc/environment | System-wide environment variables | 
Example:
echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc
π Common Environment Variables in Linux
| Variable | Description | 
|---|---|
| PATH | Directories to search for executables | 
| HOME | User’s home directory | 
| USER | Current logged-in username | 
| SHELL | Shell program path | 
| EDITOR | Default text editor | 
| LANG | Language/locale settings | 
| PWD | Present working directory | 
π Summary β Recap & Next Steps
Environment variables are critical in defining user preferences, executable paths, system language, and more. Commands like env and printenv provide easy visibility into the environment, while export and .bashrc offer customization for workflows and automation.
π Key Takeaways:
- Use envandprintenvto list or query environment variables.
- Set variables temporarily using VAR=value, and export withexport.
- Use shell config files like .bashrcto make variables permanent.
- Environment variables affect shell behavior, programming tools, and login sessions.
β FAQs
β Whatβs the difference between env and printenv?
β
 env can list variables and run commands with temporary ones. printenv only prints values of existing variables.
β How do I permanently set an environment variable?
β
 Add it to ~/.bashrc or ~/.bash_profile:
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk"
β How do I delete an environment variable in a session?
β
 Use:
unset MYVAR
β What does $PATH do in Linux?
β
 It lists directories where the shell looks for executable programs.
β Can I pass environment variables to a script?
β
 Yes. Example:
ENV_NAME=value ./script.sh
Share Now :
