Linux Environment Variables - Manage and Understand Your System's Settings

Explore and manage Linux environment variables with our comprehensive guide. Learn to set, unset, and understand common variables like HOME, PATH, and USER.

Linux Environment Variables

Understanding Linux Environment Variables

Environment variables are dynamic named values that can affect the way running processes will behave on a computer. In Linux, they play a crucial role in configuring the system and applications. This guide provides an overview of common environment variables and how to manage them.

Commonly Used Environment Variables

Variable Description
$HOME Home directory of the current user.
$PWD Present working directory.
$USER Username of the current user.
$SHELL The default login shell for the current user.
$PATH A colon-separated list of directories where the shell searches for executable commands.
$EDITOR The default text editor used by various programs.
$LANG Specifies the default language and locale settings for the system.
$HOSTNAME The hostname of the computer.

Managing Environment Variables

You can set, modify, and remove environment variables using various shell commands. Understanding these commands is essential for customizing your Linux environment.

Command Description
export VAR=value Sets an environment variable named VAR to the specified value. This makes the variable available to child processes.
export VAR=value1:value2 Sets an environment variable with multiple values, often used for path-like variables.
export VAR=$VAR:value Appends a new value to an existing environment variable, typically used for extending paths.
unset VAR Removes an environment variable.
env Displays all currently set environment variables.
printenv Similar to env, displays all environment variables. Can also display specific variables like printenv PATH.
echo $VAR Displays the value of a specific environment variable.

Advanced: Parameter Expansion

Bash shell provides powerful parameter expansion features to manipulate variable values.

Command Description
${VAR} Expands to the value of the variable VAR.
${VAR:-value} Expands to the value of VAR if it is set and not null; otherwise, expands to value. The variable VAR is not changed.
${VAR:=value} Expands to the value of VAR if it is set and not null; otherwise, value is assigned to VAR and then expanded.
${VAR:?message} Expands to the value of VAR if it is set and not null; otherwise, prints message to standard error and exits the shell.
${VAR:+value} Expands to value if VAR is set and not null; otherwise, expands to nothing.

For more in-depth information on environment variables and shell scripting, refer to the Bash Manual and MDN Web Docs on Environment Variables.