What are Environment Variables in Linux?
An environment variable in Linux is a named value stored in the system environment. It consists of a key-value pair that is accessible by the shell and all processes spawned from it.
Syntax:
VARIABLE_NAME=value
Examples:
HOME=/home/john
PATH=/usr/bin:/usr/local/bin
Environment variables provide critical information such as file locations, user identity, shell configuration, and execution paths. Programs rely on these values to determine how they should operate.

Types of Environment Variables
Local Variables
Local variables are defined within a shell session and are not inherited by child processes. They are useful for temporary configurations within a single terminal instance.
MY_VAR="Hello Linux"
Global (Exported) Variables
Global variables are made available to the current shell and all child processes using the export command. These are required when scripts or applications need access to the variable.
export MY_VAR="Hello Linux"
Shell Variables
Shell variables are predefined variables that control the behavior of the shell itself. These include variables such as PS1 for prompt customization and HISTSIZE for controlling command history size.
Common Environment Variables in Linux
| Variable | Description | Example |
|---|
| PATH | Defines directories searched for executable commands | /usr/bin:/usr/local/bin |
| HOME | Specifies the current user’s home directory | /home/username |
| USER | Represents the logged-in user | john |
| SHELL | Indicates the default shell | /bin/bash |
| PWD | Shows the current working directory | /home/john/projects |
| LANG | Defines system language and locale | en_US.UTF-8 |
| EDITOR | Specifies the default text editor | nano |
The PATH variable is particularly critical because it determines where the system looks for executable files. Incorrect modification of PATH can lead to command execution failures.
Viewing Environment Variables
Using printenv
The printenv command displays all environment variables or a specific variable.
printenv
printenv HOME
Using env
The env command lists all environment variables available in the current environment.
env
Using echo
The echo command is used to display the value of a specific variable.
echo $PATH
echo $USER
Setting Environment Variables
Temporary Variables (Session Scope)
Variables defined in the shell exist only for the duration of that session.
MY_NAME="Linux User"
export MY_NAME
Or in a single command:
export MY_NAME="Linux User"
These variables are removed automatically when the session ends.
Permanent Variables (Persistent Configuration)
To make environment variables persistent across sessions, they must be defined in shell configuration files.
Common files include:
- ~/.bashrc for interactive non-login shells
- ~/.bash_profile for login shells
- /etc/environment for system-wide variables
Example:
echo 'export MY_NAME="Linux User"' >> ~/.bashrc
source ~/.bashrc
The source command reloads the configuration file so that changes take effect immediately.

Unsetting Environment Variables
Environment variables can be removed from the current session using the unset command.
unset MY_NAME
Once unset, the variable is no longer available to the shell or child processes.
Practical Example
Consider a scenario where a project directory is frequently accessed in scripts. Instead of hardcoding the path, an environment variable can be used.
export PROJECT_DIR="/home/john/myproject"
cd $PROJECT_DIR
echo "Working in: $PROJECT_DIR"
This approach improves maintainability and reduces the risk of errors when directory paths change.
Real-World Use Cases
- Extending the PATH variable to include custom binaries
- Storing configuration values such as application ports and debug flags
- Managing credentials and API keys (with secure practices)
- Switching between development, testing, and production environments
- Customizing shell behavior and user preferences
Best Practices
- Use clear and descriptive variable names
- Limit the use of global variables to necessary cases
- Avoid exposing sensitive data in plain text configuration files
- Use user-level configuration files instead of system-wide changes when possible
- Validate changes to critical variables such as PATH
Common Mistakes
- Not exporting variables required by child processes
- Editing incorrect configuration files
- Forgetting to reload configuration files after modification
- Overwriting existing variables unintentionally
- Misconfiguring the PATH variable, leading to broken command execution
