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:
| Variable | Purpose |
| USER | Shows the logged-in username |
| HOME | Points to your main directory |
| SHELL | Displays your current shell path |
| PATH | Lists directories for executable files |
| LANG | Stores language and locale settings |
| Indicates 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:
| File | Description |
| /etc/environment | Stores global values for all users |
| /etc/profile | Runs 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.
| Type | Scope | Inheritance |
| Shell variables | Current shell only | Not passed to child processes |
| Environment variables | System-wide | Inherited by subshells |
Shell values are only active in your current terminal. Environment values transfer to any program you launch.
Quick Reference Table
| Task | Command |
| Display all values | env or printenv |
| Show specific value | printenv VARIABLE_NAME |
| Create temporary value | export NAME=value |
| Remove a value | unset NAME |
| Reload configuration | source ~/.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.