Script Valley
Linux Basics Complete Course: From Beginner to System Administrator
Module 7: Shell Scripting and EnvironmentLesson 7.1

Environment Variables in Linux | export PATH HOME

environment variables, export, PATH, HOME, printenv, env, .bashrc, .bash_profile

Environment Variables in Linux

Environment variables are named values stored in the shell's environment that influence the behavior of processes and programs. They are a fundamental part of Linux basics and are used to configure applications, set system paths, store credentials, and customize the shell experience.

Viewing Environment Variables

The printenv command displays all environment variables or the value of a specific one. The env command also prints all variables. The echo command prints the value of a specific variable by prefixing its name with a dollar sign.

printenv
printenv PATH
env
echo $HOME
echo $USER
echo $SHELL
echo $PWD

Common Environment Variables

The PATH variable contains a colon-separated list of directories that the shell searches when you type a command name. HOME is the current user's home directory. USER is the current username. SHELL is the path to the current shell. LANG and LC_ALL control language and locale settings.

echo $PATH
echo $HOME
echo $LANG

Setting and Exporting Variables

You can set a variable by assigning a value without spaces around the equals sign. To make a variable available to child processes, export it. Without export, the variable exists only in the current shell.

MY_VAR="Hello World"
echo $MY_VAR
export MY_VAR
export DB_HOST="localhost"
export DB_PORT="5432"

Adding to PATH

To add a new directory to PATH so that executables in it can be found by name, append to the existing PATH variable and export it.

export PATH="$PATH:/home/alice/scripts"
export PATH="$PATH:/usr/local/bin"

Persistent Configuration with .bashrc and .bash_profile

Variables set in the terminal are lost when the session ends. To make them permanent, add export statements to ~/.bashrc (for interactive non-login shells) or ~/.bash_profile (for login shells). After editing, reload with source.

nano ~/.bashrc
export EDITOR="nano"
export MY_API_KEY="abc123"
source ~/.bashrc

Never store sensitive credentials like API keys or passwords in .bashrc if the system is shared. Use environment-specific configuration files or secret managers in production environments.

Up next

Bash Shell Scripting for Beginners | Complete Guide

Sign in to track progress