The 10 Easiest Ways to Set Environment Variables in Linux

Introduction

This tutorial will guide you through the fundamentals of how to set environment variables in Linux, by checking your Linux version and using the Linux pipe command.

The 10 Easiest Ways to Set Environment Variables in Linux

Prerequisites To How To Set Environment Variables In Linux

Before starting, make sure you understand shell commands and scripting. It really is required to know how to use commands like export, echo, and unset. You’ll also need to know variable naming conventions and how to handle different types of variable values.

What Are Linux Environment Variables?

Environment variables in Linux are dynamic values that affect the processes or programs on a system. They are used within the Bash shell to manage configurations and preferences.

These variables can be global or local, with global variables accessible system-wide and local variables limited to the current shell session.They follow a key-value pair structure and are case-sensitive. Common examples include PATH, HOME, and SHELL.

image

How the Environment and Environmental Variables Work in Linux

In Linux environments, environment variables store system paths, user preferences, and configuration settings. You can view and modify these variables using commands like export and echo.

For instance, PATH contains directories for executable files, while HOME points to the user’s home directory.

How the Environment and Environmental Variables Work in Linux

The Most Common Environment Variables

Here’s a brief overview of some common environment variables:

  • PATH: Directories to search for executable files.
  • HOME: Location of the user’s home directory.
  • SHELL: Preferred shell path.
  • ENV: List of all environment variables.
  • UID: Unique identifier of the logged-in user.
  • TEMP: Path for temporary files.

How to List Environment Variables in Linux

printenv Command

The printenv command lists the values of the current environment variables. You can use it without arguments to list all variables or specify a particular variable to display its value.

printenv

To find a specific variable, combine printenv with grep:

printenv | grep PATH

env Command

The env command checks environment variables for the current session. It’s similar to printenv but focuses on the current shell environment.

env

set Command

The set command prints all environment and shell variables/functions. Use it with less for easier viewing.

set | less

echo Command

The echo command lists the value of specific shell variables.

echo $HOME

declare Command

The declare command lists and sets shell variables. Use it with less for detailed outputs.

declare -p | less

How to Set Environment Variables in Linux

Using export Command

To set an environment variable, use the export command:

export NAME=VALUE

For example, setting the Java home directory:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
echo $JAVA_HOME

Note that this setting is temporary and lasts only for the session. To make it persistent, you need to add it to the shell configuration file.

How to Create a Shell Variable

Creating a shell variable involves a few simple steps:

  1. Launch your terminal.
  2. Assign a value to the variable.
  3. Verify the variable with echo.
MY_VAR="Hello, World!"
echo $MY_VAR

To make variables persist across sessions, add the variable assignment to ~/.bashrc.

Set Environment Variables In Linux That Are “Persist”

To edit the .bashrc file for persistent variables:

  1. Open .bashrc with a text editor.
  2. Add your variable assignment.
  3. Save the file and restart your shell, or use the source command.
echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' >> ~/.bashrc
source ~/.bashrc
echo $JAVA_HOME

Persisting Global Environment Variables for All Users

For global persistence, edit /etc/profile.d:

  1. Create a new file for the global environment variable.
  2. Add the variable assignment.
sudo nano /etc/profile.d/java.sh

Add the following line:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Save and close the file. This will ensure that the variable is set for all users.

How to Unset an Environment Variable

To remove an environment variable, use the unset command:

unset [VARIABLE_NAME]

For permanent removal, delete the variable from the configuration files.

Conclusion

Knowing how to manage and set environment variables in Linux is crucial for efficient workflows and application performance.

FAQs

What are environment variables in Linux, and how are they used in Linux?

Environment variables are dynamic values used for configuration settings within the operating system.

How do I set environment variables in Linux?

Use the export command: export NAME=VALUE.

How do I remove un-set environment variablies in Linux?

Use the unset command: unset [VARIABLE_NAME].

How do I list all the set environment variables in Linux?

Use the printenv command.

How do I access the value of an environment variable in a shell script?

Use $ followed by the variable name.

How do I make environment variables persistent in Linux?

Set them in the shell’s configuration file (.bashrc or .bash_profile).

How do I print the value of all the environment variables?

Use the printenv command.

How do I set an environment variable to a list of directories?

Use a colon-separated list in the export command.

What is the difference between a shell variable and an environment variable in Linux?

Shell variables are local to the current shell session, while environment variables are accessible system-wide.

How do I define environment variables in Red Hat Enterprise Linux?

Set them in /etc/profile for system-wide variables.