🐧 Managing User Environment Variables in Linux

Linux is known for its flexibility, and one of the ways it achieves this is through the use of environment variables and custom configurations. Whether you’re a beginner or a power user, understanding how to manage these elements can greatly improve your productivity and system control.


🌐 Viewing and Modifying Environment Variables

Environment variables are dynamic values that affect the way processes behave on your system. You can customize these to suit your needs or automate tasks.

1. πŸ” Viewing All Environment Variables

To list all environment variables currently available in your session:

printenv

or

env

These commands show variables like HOME, PATH, SHELL, and more.


2. 🎯 Filtering for a Particular Variable

If you want to see the value of a specific variable (e.g., PATH):

echo $PATH

You can also use grep to search within all variables:

printenv | grep PATH

3. ✏️ Changing Variable Value for a Session

To temporarily change a variable’s value (for the current shell session only):

export VAR_NAME="new_value"

Example:

export EDITOR=nano

This change will disappear when the terminal session ends.


4. πŸ“ Making Variable Value Change Permanent

To make an environment variable persistent across sessions, add it to one of the shell config files:

  • For bash users, add the export command to:
    • ~/.bashrc (for interactive shells)
    • ~/.profile or ~/.bash_profile (for login shells)

Example:

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

For zsh users, use ~/.zshrc instead.


πŸ’‘ Changing Your Shell Prompt

Your shell prompt is controlled by the PS1 variable.

Example:

PS1="[\u@\h \W]\$ "

This sets your prompt to show:

  • Username (\u)
  • Hostname (\h)
  • Current directory (\W)

To make it permanent:

echo 'PS1="[\u@\h \W]\$ "' >> ~/.bashrc
source ~/.bashrc

🧭 Changing Your PATH

The PATH variable defines where the shell looks for executable files.

1. βž• Adding a Path to Variables

export PATH=$PATH:/opt/myapp/bin

To make it permanent:

echo 'export PATH=$PATH:/opt/myapp/bin' >> ~/.bashrc
source ~/.bashrc

2. ❌ How Not to Add a Path to Variables

Avoid overwriting the entire path accidentally:

export PATH=/opt/myapp/bin   # ⚠️ BAD! This removes all previous paths

Instead, append or prepend to maintain system functionality.


πŸ§ͺ Creating User-Defined Variables

You can define your own environment variables for scripts or session settings.

MY_NAME="Raj"
echo $MY_NAME

To persist:

echo 'export MY_NAME="Raj"' >> ~/.bashrc

These variables are useful in automation scripts and aliases.


πŸ“Œ Summary

TaskCommand / File
View all environment varsprintenv or env
View one variableecho $VAR_NAME
Temporary changeexport VAR_NAME="value"
Permanent changeAdd to ~/.bashrc or ~/.profile
Change shell promptSet PS1
Modify PATH correctlyexport PATH=$PATH:/new/path

πŸ“ Exercises

  1. Use printenv to list all environment variables.
  2. Create a new variable MY_PROJECT and set it to your favorite folder.
  3. Append /usr/local/bin to your PATH without overwriting it.
  4. Modify your prompt to display only your username and current directory.
  5. Make a variable persistent and test it in a new terminal session.

βœ… Bonus Tip:

You can manage system-wide environment variables in:

/etc/environment
/etc/profile
/etc/profile.d/*.sh

Use these with caution, especially on production systems.

Leave a Reply

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

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>