Every Linux system relies on special values called environment variables. These help programs and the operating system work smoothly.

Learning how to Linux set environment variable is essential for any user or developer. This manual covers everything you need to know.

What Are These Special Values?

Environment variables store important data for your system. They hold paths, user details, and program settings. Each session or user can have unique values.

Here are some common ones:

VariablePurpose
USERShows the logged-in username
HOMEPoints to your main directory
SHELLDisplays your current shell path
PATHLists directories for executable files
LANGStores language and locale settings
MAILIndicates mail storage location

These values change based on your active session. Understanding them helps you customize your Linux experience.

Displaying Current Values

Two methods exist for viewing these values.

Method 1: Using printenv

This command shows one or all values:

bash

printenv HOME

```

Output:

```

/home/username

Method 2: Using echo

Add a dollar sign before the name:

bash

echo $SHELL

```

Output:

```

/bin/bash

To view every value at once, simply run env or printenv without arguments.

How to Linux Set Environment Variable?

The export command helps you set the Linux environment variable quickly. Here’s the basic format:

bash

export VARIABLE_NAME=value

Example:

Create a new value called JAVA_HOME:

bash

export JAVA_HOME=/usr/bin/java

Verify it works:

bash

echo $JAVA_HOME

```

Output:

```

/usr/bin/java

This approach works great for temporary needs. However, closing your terminal erases these values.

Making Values Permanent

When you Linux set environment variable using export, it only lasts for one session. For permanent storage, modify configuration files.

For Individual Users:

Edit the .bashrc file in your home folder:

bash

vi ~/.bashrc

Add your export statement at the bottom:

bash

export JAVA_HOME=/usr/bin/java

Save and reload the file:

bash

source ~/.bashrc

Now your value persists across sessions. 

Creating System-Wide Values

Sometimes, every user needs access to certain values. Two files handle this:

FileDescription
/etc/environmentStores global values for all users
/etc/profileRuns during bash login sessions

Editing /etc/environment:

bash

sudo vi /etc/environment

```

Add your value:

```

GLOBAL_VAR="This works everywhere"

Editing /etc/profile:

bash

sudo vi /etc/profile

Use the export command:

bash

export GLOBAL_VAR="Available to everyone"

Apply changes immediately:

bash

source /etc/environment

All users can now access this value. For more advanced configuration, explore commandlinux.com/linux-admin.

Key Differences: Shell vs Environment Values

Understanding both types matters when setting the Linux environment variable.

TypeScopeInheritance
Shell variablesCurrent shell onlyNot passed to child processes
Environment variablesSystem-wideInherited by subshells

Shell values are only active in your current terminal. Environment values transfer to any program you launch.

Quick Reference Table

TaskCommand
Display all valuesenv or printenv
Show specific valueprintenv VARIABLE_NAME
Create temporary valueexport NAME=value
Remove a valueunset NAME
Reload configurationsource ~/.bashrc

Final Thoughts

Knowing how to set Linux environment variables gives you greater control over your system. Start with temporary values using export.

Move to permanent storage in configuration files when needed. Global values help multiple users share important settings.

Practice these commands regularly. They form the foundation for advanced Linux administration and development work.

Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.