Hey everyone, today we will see How to add Environment Variable Linux. We will cover this topic in detail with code and live tutorials.
Environment variables in Linux are important in your configuration and automation. They hold important information that the various programs and scripts will refer to to run correctly. We will discuss dealing with both temporary and permanent environment variables under Linux. You’ll see how you can change your system and how it behaves just the way you need it. Let’s run through an explanation of what environment variables are, how to manage them, and best practices on how this should be done within a Linux environment.
What Are Environment Variables in Linux?
Environment variables are dynamic key-value pairs that describe the context in which the applications, scripts, or system processes run. The variables may apply to everything in the system or even a single application. For example:
- The PATH variable stipulates where the system should look for executable files.
- The HOME variable holds the path to your home directory.
With this understanding and management of environment variables in Linux, one can smooth out workflows and align the system with his desire.
Also Read: How To Install Truecaller In Kali Linux
How to View Current Environment Variables
Before adding the new environment variable, we need to check out which variables are already in our system.
env
This command will display a list of all environment variables currently active in your session.
To see a specific one you can use the below command:
echo $VARIABLE_NAME
For example, to view the value of the PATH variable, you would use:
echo $PATH
How To Add Environment Variable Using printenv
We can also use the alternate way which is printenv.
printenv
You can also filter the environment variable list with grep:
env | grep VARIABLE_NAME
Command | Action |
---|
env | Shows all environment variables |
printenv | Lists all environment variables |
echo $VARIABLE | Displays the value of a specific variable |
`env | grep VAR` |
Adding Environment Variables in Linux
There are two primary ways to set environment variables in Linux:
- Temporarily: Variables exist only during the current session.
- Permanently: Variables persist across system reboots.
1. Adding Temporary Environment Variables
To add a temporary environment variable, use the export
command:
bashCopy codeexport VARIABLE_NAME=VALUE
This sets the environment variable only for the current shell session. Once you close the terminal, the variable will no longer exist.
Example:
If you want to set a variable my_color
to blue
, run:
bashCopy codeexport my_color=blue
You can verify it by running:
bashCopy codeecho $my_color
2. Adding Permanent Environment Variables
Permanent environment variables remain available in all future shell sessions and persist across system reboots. Hereโs how to set them:
Method 1: Modify .bashrc
or .bash_profile
(User-Specific)
For variables specific to your user, you can add them to your shellโs startup files like .bashrc
or .bash_profile
(for bash users).
- Open the
.bashrc
file using a text editor:bashCopy codenano ~/.bashrc
- Add the
export
command at the end of the file:bashCopy codeexport VARIABLE_NAME=VALUE
For example:bashCopy codeexport my_color=blue
- Save the file and apply the changes:bashCopy code
source ~/.bashrc
Method 2: Edit /etc/environment
(System-Wide)
To make environment variables available for all users, you can modify the /etc/environment
file.
- Open
/etc/environment
with root permissions:bashCopy codesudo nano /etc/environment
- Add your variable in the format:bashCopy code
VARIABLE_NAME=VALUE
Example:bashCopy codemy_color=blue
- Save and close the file, then reboot your system to apply the changes.
Best Practices for Managing Environment Variables
- Case Sensitivity: Environment variable names are case-sensitive, meaning
MY_VAR
andmy_var
are different. - Use Quotes for Values with Spaces: If your value contains spaces, use quotes.bashCopy code
export MY_PATH="/home/user/bin"
- Avoid Naming Conflicts: Try not to override system-defined environment variables unless necessary.
- Unsetting Variables: To remove a variable from your current session, use the
unset
command:bashCopy codeunset VARIABLE_NAME
Common Environment Variables in Linux
Here are some frequently used environment variables that you may encounter:
Variable | Description |
---|---|
PATH | Locations where executables are searched |
HOME | Path to the userโs home directory |
SHELL | The userโs default shell |
USER | The name of the current user |
LANG | System language settings |
Conclusion
We Have successfully learned How To Add Environment Variable Linux using various methods. If you are facing any errors or issues then let me know in the comment section I will try to help you with some troubleshooting guides. Don’t forget to rate and share this post. Thanks for reading
FAQs
What Are Environment Variables in Linux, and Why Are They Important?
Environment variables store configuration settings that affect how programs run. They provide crucial data needed for programs and scripts to run dynamically.
How Can I Display the Current Linux Environment Variables?
You can print all environment variables that are currently set with the env or printenv commands. To address a particular variable, use the echo $VARIABLE_NAME command
How Do I Set a New Environment Variable in Linux?
To define a single variable temporarily you might use the export command, such as:export VARIABLE_NAME=value
To make it permanent append variable to your .bashrc file or /etc/environment.
Can I change or remove an existing environment variable?
Yes you can change the variables by exporting again but this time using new values. You can unset an environment variable for the current session with, unset VARIABLE_NAME.