How to set and modify environment variables in Linux?

LinuxLinuxBeginner
Practice Now

Introduction

Linux environment variables are essential for configuring and customizing your system's behavior. In this tutorial, we will explore the process of setting, modifying, and managing environment variables in the Linux operating system, equipping you with the knowledge to streamline your programming and system administration tasks.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicSystemCommandsGroup -.-> linux/declare("`Variable Declaring`") linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") linux/UserandGroupManagementGroup -.-> linux/env("`Environment Managing`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") linux/UserandGroupManagementGroup -.-> linux/unset("`Variable Unsetting`") subgraph Lab Skills linux/declare -.-> lab-409912{{"`How to set and modify environment variables in Linux?`"}} linux/source -.-> lab-409912{{"`How to set and modify environment variables in Linux?`"}} linux/env -.-> lab-409912{{"`How to set and modify environment variables in Linux?`"}} linux/set -.-> lab-409912{{"`How to set and modify environment variables in Linux?`"}} linux/export -.-> lab-409912{{"`How to set and modify environment variables in Linux?`"}} linux/unset -.-> lab-409912{{"`How to set and modify environment variables in Linux?`"}} end

Introduction to Environment Variables in Linux

Environment variables in Linux are a set of key-value pairs that are used to configure the operating system and applications. They provide a way for the system and programs to store and retrieve configuration information, such as file paths, user preferences, and system settings.

Environment variables are essential for managing the runtime environment of a Linux system and its applications. They can be accessed and modified by both the operating system and individual programs, allowing for dynamic configuration and customization.

What are Environment Variables?

Environment variables are named values that are stored in the operating system's environment. They can be accessed and used by the shell, scripts, and applications running on the system. Environment variables are typically defined as a name-value pair, where the name is a unique identifier, and the value is the data associated with that identifier.

Importance of Environment Variables

Environment variables play a crucial role in the following areas:

  1. System Configuration: Environment variables are used to store system-wide configuration settings, such as the location of system directories, default file permissions, and network settings.

  2. Application Configuration: Applications often use environment variables to store their own configuration data, such as database connection strings, API keys, and user preferences.

  3. Scripting and Automation: Environment variables are extensively used in shell scripts and automation tools to pass information between different components of a system.

  4. User Customization: Users can set their own environment variables to personalize their working environment, such as setting the default editor or preferred command-line tools.

Accessing and Modifying Environment Variables

Environment variables can be accessed and modified using various commands and techniques in the Linux shell. The most common commands for working with environment variables are export, set, and unset.

## Display all environment variables
env

## Set an environment variable
export MY_VARIABLE="Hello, LabEx!"

## Modify an existing environment variable
export MY_VARIABLE="Goodbye, LabEx!"

## Unset an environment variable
unset MY_VARIABLE

In the next section, we will dive deeper into the process of setting and modifying environment variables in Linux.

Setting Environment Variables in Linux

There are several ways to set environment variables in Linux, depending on the scope and duration of the variable.

Temporary Environment Variables

To set a temporary environment variable that only lasts for the current shell session, you can use the export command:

export MY_VARIABLE="Hello, LabEx!"

This will create a new environment variable named MY_VARIABLE with the value "Hello, LabEx!". The variable will be available in the current shell session and any child processes (such as scripts or subshells) that are spawned from it.

Persistent Environment Variables

To make an environment variable persistent across login sessions, you need to add it to a configuration file that is loaded when the shell starts. The most common files for this purpose are:

  • ~/.bashrc: For Bash shell users
  • ~/.zshrc: For Zsh shell users

Here's an example of how to add a persistent environment variable to the ~/.bashrc file:

## Add this to the end of your ~/.bashrc file
export MY_VARIABLE="Hello, LabEx!"

After saving the file and restarting the shell (or running source ~/.bashrc), the MY_VARIABLE environment variable will be available in all subsequent shell sessions.

System-wide Environment Variables

In addition to user-specific environment variables, you can also set system-wide environment variables that are accessible to all users on the system. These variables are typically defined in system-level configuration files, such as /etc/environment or /etc/profile.d/*.sh.

Here's an example of how to set a system-wide environment variable in the /etc/environment file:

## Add this to the /etc/environment file
MY_VARIABLE="Hello, LabEx!"

After saving the file and restarting the system, the MY_VARIABLE environment variable will be available to all users and processes on the system.

Remember that system-wide environment variables should be used with caution, as they can affect the entire system and may have unintended consequences if not properly configured.

Modifying and Managing Environment Variables

Once you have set environment variables, you may need to modify or manage them as your system or application requirements change. Here are some common techniques for modifying and managing environment variables in Linux.

Modifying Environment Variables

To modify the value of an existing environment variable, you can simply use the export command again with the new value:

## Modify the value of an existing environment variable
export MY_VARIABLE="Goodbye, LabEx!"

This will update the value of the MY_VARIABLE environment variable to the new string "Goodbye, LabEx!".

Unsetting Environment Variables

If you no longer need an environment variable, you can remove it using the unset command:

## Remove an environment variable
unset MY_VARIABLE

After running this command, the MY_VARIABLE environment variable will be removed from the current shell session.

Viewing Environment Variables

To view all the environment variables currently set in your shell, you can use the env or printenv commands:

## Display all environment variables
env
printenv

This will output a list of all the environment variables and their corresponding values.

You can also view the value of a specific environment variable using the echo command:

## Display the value of a specific environment variable
echo $MY_VARIABLE

Managing Environment Variables in Configuration Files

If you need to make changes to persistent environment variables (those defined in configuration files like ~/.bashrc or /etc/environment), you can edit the corresponding file directly. After making the changes, you'll need to either log out and log back in or run source <file> to apply the changes.

For example, to modify the value of the MY_VARIABLE environment variable in the ~/.bashrc file, you would edit the file and change the line:

export MY_VARIABLE="Hello, LabEx!"

to the new value:

export MY_VARIABLE="Goodbye, LabEx!"

Then, run source ~/.bashrc to apply the changes to the current shell session.

By understanding these techniques for modifying and managing environment variables, you can effectively customize and maintain the runtime environment of your Linux system and applications.

Summary

By the end of this tutorial, you will have a comprehensive understanding of working with environment variables in Linux. You will learn how to set, modify, and manage these crucial system settings, enabling you to customize your Linux environment and optimize your workflow. Whether you are a developer, system administrator, or a Linux enthusiast, this guide will provide you with the necessary skills to effectively manage environment variables in your Linux system.

Other Linux Tutorials you may like