fbpx
Join WhatsApp
Join Now
Join Telegram
Join Now

How To Add Environment Variable Linux | Detailed Guide 2024

By Noman

Published on:

How To Add Environment Variable Linux
5/5 - (1 vote) ๐Ÿ’– THANKS

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:

How To Add Environment Variable Linux
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
CommandAction
envShows all environment variables
printenvLists all environment variables
echo $VARIABLEDisplays the value of a specific variable
`envgrep VAR`

Adding Environment Variables in Linux

There are two primary ways to set environment variables in Linux:

  1. Temporarily: Variables exist only during the current session.
  2. 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).

  1. Open the .bashrc file using a text editor:bashCopy codenano ~/.bashrc
  2. Add the export command at the end of the file:bashCopy codeexport VARIABLE_NAME=VALUE For example:bashCopy codeexport my_color=blue
  3. Save the file and apply the changes:bashCopy codesource ~/.bashrc

Method 2: Edit /etc/environment (System-Wide)

To make environment variables available for all users, you can modify the /etc/environment file.

  1. Open /etc/environment with root permissions:bashCopy codesudo nano /etc/environment
  2. Add your variable in the format:bashCopy codeVARIABLE_NAME=VALUE Example:bashCopy codemy_color=blue
  3. 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 and my_var are different.
  • Use Quotes for Values with Spaces: If your value contains spaces, use quotes.bashCopy codeexport 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:

VariableDescription
PATHLocations where executables are searched
HOMEPath to the userโ€™s home directory
SHELLThe userโ€™s default shell
USERThe name of the current user
LANGSystem 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.

Iโ€™m Noman Mohammad, a dedicated freelancer with a passion for technology and a commitment to delivering exceptional digital solutions. With over a year of hands-on experience in WordPress development and content writing, Iโ€™ve developed a unique skill set that allows me to create dynamic, user-friendly websites that not only look great but also perform exceptionally well.

Leave a Comment