Master Linux Environment Variables: View, Set, and Use Them in Bash
This guide explains what Linux environment variables are, how to display them with env or printenv, set new variables using export, make them read‑only, unset them, access them from C programs with getenv/setenv/unsetenv, and modify system configuration files to persist changes.
1. Overview
Linux is a multi‑user operating system. Each logged‑in user gets a dedicated environment defined by a set of variables called environment variables. Users can modify their own environment variables to suit their needs. Environment variables are closely tied to the shell and are set via shell commands; they are accessible to all programs run by the current user. In Bash, variables can be accessed by their names.
2. Display
You can view all environment variables with the env or printenv commands.
To display the value of a specific variable, use echo, e.g.:
echo $HOME
/home/admin3. Set a New Environment Variable
export NAME=" Example "
echo $NAME
Example4. Use unset to Remove an Environment Variable
unset NAME
echo $NAME5. Use readonly to Set a Read‑Only Variable
export NAME=" Example "
readonly NAME
unset NAME
-bash: unset: NAME: cannot unset: readonly variable
NAME=" New " # This will also fail to modify the variable
-bash: TEST: readonly variable6. Access and Set Environment Variables in C Programs
C programs can use three functions to work with environment variables:
getenv() – retrieves the value of a variable; returns NULL if the variable does not exist.
setenv() – sets a variable in the program.
unsetenv() – removes a specific variable.
There is also a global pointer environ that points to the list of all environment variables. The following program prints every environment variable:
#include <stdio.h>
extern char **environ;
int main() {
char **var;
for (var = environ; *var != NULL; ++var)
printf("%s
", *var);
return 0;
}7. Modify System Configuration Files
To make environment variable changes persistent, edit the relevant configuration files such as /etc/profile for the root user or ~/.bash_profile for regular users (create the file if it does not exist). After editing, log out and back in or run source <file> to apply the changes.
8. Common Environment Variables
PATH – directories the shell searches for commands. HOME – current user's home directory. TERM – terminal type. UID – numeric user identifier. PWD – current working directory (updates with cd ). HISTSIZE – number of commands stored in history. LOGNAME – login name of the current user. HOSTNAME – name of the host machine. SHELL – type of the user's shell. LANGUAGE – language‑related settings for localization. MAIL – directory where the user's mail is stored. PS1 – primary command‑line prompt ("#" for root, "$" for normal users). PS2 – secondary prompt (default ">").
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
