Fundamentals 11 min read

Master Linux Environment and Shell Variables: Definitions, Commands, and Persistence

This guide explains the difference between Linux environment and shell variables, shows how to view, set, export, and unset them with commands like env, printenv, export, unset, and demonstrates how to make variables persistent by editing common startup files such as /etc/profile and ~/.bashrc.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment and Shell Variables: Definitions, Commands, and Persistence

What Are Environment and Shell Variables?

In Linux, environment variables are key‑value pairs available to a program and all of its child processes, often used to store defaults like the editor, browser, or executable paths. Shell variables exist only in the current shell session and can hold temporary data such as the current user ID.

Syntax of Environment Variables

Single‑value variables are written as: KEY=value1 Multiple values are separated by a colon: KEY=value1:value2:value3 If a value contains spaces, wrap it in quotes:

KEY="value with spaces"

Common Environment and Shell Variables

Some frequently used variables include: TERM: terminal type USER: logged‑in user PWD: current working directory PATH: directories searched for executables LANG: locale and encoding

Typical shell‑specific variables are: BASHOPTS: enabled Bash options BASH_VERSION: human‑readable Bash version COLUMNS: width of output HISTSIZE: number of commands kept in history PS1: primary prompt string

Viewing Variables

Use the following commands: env or printenv: list all environment variables set: list both environment and shell variables plus functions

Example to query a single variable: $ printenv SHELL Output:

/bin/bash

Setting and Exporting Variables

Define a shell variable in the current session: $ MYVAR=lxlinux Export it to become an environment variable: $ export MYVAR Verify: $ printenv MYVAR To set a variable with a value containing spaces in one line: $ export MYNEWVAR="My New Variable" To remove a variable entirely, use unset:

$ unset MYVAR

Persisting Environment Variables

To avoid re‑defining variables each session, add them to startup files that Bash reads: /etc/environment: system‑wide variables /etc/profile: variables for login shells ~/.bashrc: user‑specific variables

After editing, load the changes with:

$ source ~/.bashrc

Conclusion

The article covered the essential concepts, common examples, and practical commands for managing Linux environment and shell variables, as well as how to make them persistent across sessions, providing a solid foundation for everyday command‑line work.

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.

bashcommand-lineshell-variablesenvironment-variables
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.