Fundamentals 10 min read

Master Linux Environment Variables: Types, Commands, and Configuration Files

This guide explains what environment variables are, how they are classified, common variables like PATH and LD_LIBRARY_PATH, essential shell commands for viewing and modifying them, and the key configuration files that control their behavior on Linux systems.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment Variables: Types, Commands, and Configuration Files

What are environment variables?

Environment variables are key‑value pairs stored by the operating system that define the runtime environment for a user session. They are kept in a table in memory and are created for each user at login.

Classification of environment variables

By lifecycle

Permanent : Defined in startup scripts (e.g., /etc/profile, ~/.bash_profile) and persist across logins.

Temporary : Defined with export in the current shell; disappear when the shell exits.

By scope

System‑wide : Visible to all users (e.g., variables set in /etc/profile).

User‑specific : Visible only to the defining user (e.g., variables set in ~/.profile).

Common environment‑variable commands

echo

Print the value of a specific variable.

echo $HOME

env

List all environment variables for the current process.

env

export

Define a new environment variable or modify an existing one. The variable becomes part of the environment of subsequently launched processes.

export MY_VAR="value"

set

Display both shell variables and environment variables.

set

unset

Remove a variable from the environment.

unset MY_VAR

printenv

Show the value of a specific variable.

printenv PATH

Typical environment variables

HOME        # User's home directory
PWD         # Current working directory
SHELL       # Path to the current shell executable
HISTSIZE    # Number of commands stored in history
HOSTNAME    # System host name
LOGNAME     # Login name of the user
LANG / LANGUAGE # Locale settings

PATH

Colon‑separated list of directories searched for executable programs. A trailing dot ( .) represents the current directory.

# Default example
export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:$PATH

# Prepend a custom directory
export PATH=$HOME/bin:$PATH

LD_LIBRARY_PATH

Colon‑separated list of directories searched by the dynamic linker for shared libraries (C/C++).

export LD_LIBRARY_PATH=/opt/lib:/usr/local/lib:$LD_LIBRARY_PATH

C_INCLUDE_PATH & CPLUS_INCLUDE_PATH

Specify additional search paths for C and C++ header files.

export C_INCLUDE_PATH=/my/include
export CPLUS_INCLUDE_PATH=/my/include_cpp

CLASSPATH

Colon‑separated list of directories or JAR files that the Java Virtual Machine searches for class files.

export CLASSPATH=.:$HOME/lib/my.jar:$CLASSPATH

Configuration files for environment variables

/etc/profile

System‑wide file executed once for each user at the first login. It typically sources scripts in /etc/profile.d. After editing, apply changes with source /etc/profile.

/etc/profile.d

Directory containing scripts that /etc/profile runs at each startup. Placing a .sh file here is the recommended way to add system‑wide variables.

/etc/bashrc (or /etc/bash.bashrc on Ubuntu)

Executed for every interactive Bash session. Useful for system‑wide aliases and functions.

User login files (~/.bash_profile, ~/.bash_login, ~/.profile)

Only one of these files is read at login, in the order shown. They are the place to set user‑specific environment variables.

~/.bashrc

Read for each new interactive shell. Commonly used for per‑user aliases, functions, and variables that should be available in every terminal.

~/.bash_logout

Executed when a Bash login shell exits; useful for cleanup commands.

Execution order

1. /etc/profile
2. /etc/bashrc (or /etc/bash.bashrc)
3. /etc/profile.d/*
4. ~/.bash_profile → ~/.bash_login → ~/.profile (first existing file)
5. ~/.bashrc

Programmatic access to environment variables

Global variable environ

In C, extern char **environ; provides direct access to the environment array.

Command‑line arguments

Variables can be passed to a program via the command line, e.g., VAR=value ./myprog.

getenv()

The standard C library function getenv() retrieves the value of a named variable.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char *pwd = getenv("PWD");
    if (pwd == NULL) {
        perror("getenv");
    } else {
        printf("Current directory: %s
", pwd);
    }
    return 0;
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ConfigurationLinuxcommands
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.