π§ 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
Task | Command / File |
---|---|
View all environment vars | printenv or env |
View one variable | echo $VAR_NAME |
Temporary change | export VAR_NAME="value" |
Permanent change | Add to ~/.bashrc or ~/.profile |
Change shell prompt | Set PS1 |
Modify PATH correctly | export PATH=$PATH:/new/path |
π Exercises
- Use
printenv
to list all environment variables. - Create a new variable
MY_PROJECT
and set it to your favorite folder. - Append
/usr/local/bin
to yourPATH
without overwriting it. - Modify your prompt to display only your username and current directory.
- 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.