πŸ“‘Linux/Unix: Permissions & Ownership
Estimated reading: 3 minutes 272 views

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 env and printenv
  • 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:

FilePurpose
~/.bashrcUser shell session (interactive)
~/.bash_profileUser login shell (login)
/etc/environmentSystem-wide environment variables

Example:

echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc

Common Environment Variables in Linux

VariableDescription
PATHDirectories to search for executables
HOMEUser’s home directory
USERCurrent logged-in username
SHELLShell program path
EDITORDefault text editor
LANGLanguage/locale settings
PWDPresent 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 env and printenv to list or query environment variables.
  • Set variables temporarily using VAR=value, and export with export.
  • Use shell config files like .bashrc to 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 :
Share

πŸ”΅ Linux/Unix: Environment Variables (env, printenv)

Or Copy Link

CONTENTS
Scroll to Top