Which file contains environment variables in Linux: A Comprehensive Guide
If you're new to the world of Linux or even if you've been using it for a while, you might have encountered the term "environment variables." These are dynamic, named values that can affect the way running processes behave on a computer. They're essential for everything from telling your system where to find executable programs to defining how your graphical interface looks. But a common question that arises is: Which file contains environment variables in Linux? The answer, however, isn't a single, simple file. Instead, it's a collection of files and configuration methods, each serving a different purpose and affecting different scopes of your Linux system.
Understanding the Scope of Environment Variables
Before diving into specific files, it's crucial to understand that environment variables in Linux can be set at different levels:
- System-wide: These variables affect all users and all processes on the system.
- User-specific: These variables affect only a particular user and their processes.
- Shell-specific: These variables are set for a particular shell session and might not persist after the session ends.
The location and method of setting these variables depend heavily on this scope.
Key Files and Locations for Environment Variables
Let's break down the most common places where you'll find environment variables defined in a Linux system:
1. System-Wide Configuration Files
These files are typically located in the /etc/ directory and require administrator (root) privileges to modify.
- `/etc/environment`: This is perhaps the most straightforward system-wide configuration file. It's a simple file containing variable assignments, with one variable per line in the format
VARIABLE=value. These variables are generally available to all users and processes upon system startup. - `/etc/profile`: This script is executed by login shells. It's a shell script, meaning it can contain more complex logic than just simple variable assignments. Many system administrators place system-wide environment variable definitions and other initialization commands here.
- Files in `/etc/profile.d/`: Instead of putting everything directly into `/etc/profile`, it's common practice to place individual configuration scripts into the `/etc/profile.d/` directory. Each file in this directory (e.g., `java.sh`, `bash_completion.sh`) is executed by login shells in alphanumeric order. This makes it easier to manage different sets of environment variables for various applications or system components.
2. User-Specific Configuration Files
These files are located in the home directory of each user (e.g., `/home/yourusername/`). They affect only the specific user who owns them and are typically executed when that user logs in or starts a new shell.
- `~/.bash_profile`: This file is executed for login shells for users using the Bash shell (which is the default for most Linux distributions). Similar to `/etc/profile`, it can contain environment variable definitions and other commands.
- `~/.bash_login`: If `~/.bash_profile` does not exist, Bash will look for `~/.bash_login`. The functionality is the same.
- `~/.profile`: If neither `~/.bash_profile` nor `~/.bash_login` exists, Bash will look for `~/.profile`. This file is a more general configuration file for login sessions and is also read by other shells.
- `~/.bashrc`: This file is executed for non-login shells (e.g., when you open a new terminal window within a graphical session). It's commonly used for setting interactive shell options and, importantly, defining environment variables that you want available in all your terminal sessions. Many users will source their `~/.bash_profile` or other files from within their `~/.bashrc` to ensure consistency.
Important Note: The exact behavior and which files are read can vary slightly between different Linux distributions and shell types. However, the files mentioned above are the most common and widely recognized.
3. Shell-Specific Variables (Temporary)
You can also set environment variables directly within your current shell session using the export command. These variables are temporary and will be lost when you close the shell session.
For example:
export MY_CUSTOM_VAR="Hello World" echo $MY_CUSTOM_VAR
This is useful for testing or for setting variables for a specific task without permanently altering configuration files.
4. Other Configuration Methods
- `/etc/default/grub`: While not strictly for user-level environment variables, this file can be used to pass kernel parameters and environment variables to the kernel during boot time.
- Systemd Unit Files: For services managed by systemd, environment variables can be defined directly within their unit files using the
Environment=directive or by referencing an environment file withEnvironmentFile=.
How to Check Your Current Environment Variables
To see all the environment variables that are currently set for your session, you can use the env command or the printenv command:
env # or printenv
To check a specific variable, you can use:
echo $VARIABLE_NAME
FAQ Section
How do I set a system-wide environment variable?
To set a system-wide environment variable, you'll typically edit files like /etc/environment or scripts within /etc/profile.d/. These actions require root privileges. After making changes, you'll usually need to log out and log back in, or reboot the system, for the changes to take full effect.
Why is my environment variable not showing up after I set it?
This is often due to the scope of the variable or the shell session. If you set it in ~/.bashrc, it will only be available in new terminal windows, not necessarily in your login session. If you set it directly in the terminal, it's temporary. Ensure you are editing the correct file for the scope you intend and that you've reloaded your shell or logged out and back in.
What's the difference between `~/.bash_profile` and `~/.bashrc`?
`~/.bash_profile` is read by Bash for login shells (e.g., when you first log in via SSH or a TTY). `~/.bashrc` is read by Bash for interactive non-login shells (e.g., when you open a new terminal window). Many users configure their `~/.bash_profile` to source their `~/.bashrc` to ensure that variables and aliases set in `~/.bashrc` are available in login shells as well.
Can I set environment variables for specific applications?
Yes, you can. One common way is to create a script in /etc/profile.d/ that sets variables for a particular application. For user-specific applications, you can define these variables in your user configuration files like `~/.bashrc` or `~/.profile`. Some applications also have their own configuration files where environment variables can be set.

