πŸ“‘Linux/Unix: Permissions & Ownership
Estimated reading: 3 minutes 38 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

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

Or Copy Link

CONTENTS
Scroll to Top