
If you’re working in a Linux environment, chances are you’ve encountered environment variables—even if you didn’t realize it at the time. They quietly power much of what goes on behind the scenes in your shell sessions, influencing everything from what shell prompt you see to which programs are available when you type a command. Whether you’re an experienced sysadmin or a new Linux user, mastering environment variables is essential for customizing and controlling your shell experience.
In this guide, we’ll take a dive into environment variables in the Linux shell. By the end, you’ll not only know how to view and set these variables, but also how to persist them, use them in scripts, and troubleshoot issues effectively.
What Are Environment Variables?
At a basic level, environment variables are dynamic named values that affect the behavior of running processes on your Linux system. Think of them as configuration settings that your shell (like Bash or Zsh) and applications refer to in order to understand how they should operate.
For example:
The
PATH
variable tells the shell where to look for executable files.The
HOME
variable stores the path to your home directory.The
LANG
variable defines your system’s language and character encoding.
Environment Variables vs Shell Variables
There is an important distinction between shell variables and environment variables:
Shell variables are local to the shell session in which they are defined.
Environment variables are shell variables that have been exported, meaning they are inherited by child processes spawned from the shell.
Viewing Environment Variables
Before you can modify or use environment variables, it’s important to know how to inspect them.
View All Environment Variables
printenv
or
env
Both commands list environment variables currently set for the session.
View a Specific Variable
echo $HOME
This will display the current user’s home directory.
View All Shell Variables
set
This command displays all shell variables and functions. It’s broader than printenv
.
Setting and Exporting Environment Variables
You can define your own variables or temporarily change existing ones within your shell.
Source: Read More