Setting Environment Variables in Linux: .bashrc vs /etc/profile

Understanding Environment Variables

Environment variables are parameters that define the operating enviroment for processes and users. The most frequantly used example is PATH, which tells the system where to locate executable files when you type a command without specifying its full directory path.

For instance, after installing Java on Windows, attempting to run java or javac from the command prompt fails unless the JDK's bin directory is added to the PATH variable.

Two Methods for Configuring Environment Variables in Linux

Method 1: Configuring .bashrc in the Home Directory

The .bashrc file stores user-specific configurations including aliases and environment variables.

Step 1: Navigate to your home directory

cd ~

Step 2: Open .bashrc for editing

vi .bashrc

Step 3: Append environment variables at the end of the file

SDK_ROOT=/opt/maven-3.9.0
BUILD_PATH=./target
PATH=$SDK_ROOT/bin:$PATH
export SDK_ROOT BUILD_PATH PATH

Step 4: Save and exit

:wq

Step 5: Apply changes immediately

source .bashrc

Without the source command, changes take effect only after a system reboot.

Method 2: Modifying /etc/profile

The /etc/profile file initializes environment settings for all users when they first log in. Changes to this file affect the entire system's environment.

Step 1: Open /etc/profile with root privileges

sudo vim /etc/profile

Step 2: Add environment variables at the end of the file

SDK_ROOT=/opt/maven-3.9.0
BUILD_PATH=./target
PATH=$SDK_ROOT/bin:$PATH
export SDK_ROOT BUILD_PATH PATH

Step 3: Save and exit

:wq

Step 4: Activate the changes

source /etc/profile

Similarly, without source, a reboot is requirde for changes to take effect.

Comparing the Two Approaches

Modifying .bashrc restricts environment variables to individual users, making it a safer choice for personal configurations. Editing /etc/profile applies settings system-wide, giving all users access to those variables.

The first method provides better isolation and reduces potential security risks. The second method is more convenient when you need consistent environment setup across all user accounts.

Recommendation: Use .bashrc for development workstations used by multiple people or when you need user-specific configurations. Reserve /etc/profile modifications for single-user systems where universal settings are necessary.

Tags: Linux Environment Variables bashrc profile System Configuration

Posted on Tue, 19 May 2026 04:41:23 +0000 by sogno