Skip to main content

Command Palette

Search for a command to run...

Environment Variables — User Environments and Configuration

Published
•4 min read•View as Markdown

By now, you’ve learned many Linux concepts:

  • Linux commands

  • Users and permissions

  • Shell scripting and automation

In this lecture, we focus on a fundamental concept shared by all operating systems:
environment variables.

Understanding environment variables is essential for:

  • Multi-user systems

  • Application configuration

  • Secure handling of sensitive data

  • DevOps automation


Multi-User Environments and User Isolation

Linux, like Windows and macOS, is a multi-user operating system.

This means:

  • Multiple users can exist on the same system

  • Each user runs programs and processes independently

  • Each user has their own environment

Every user can customize:

  • UI preferences

  • Fonts and colors

  • Default shell

  • Default editor

  • Default web browser

These settings must not affect other users.

So how does the operating system keep track of:

  • User preferences?

  • User-specific configurations?

  • User-specific execution environments?

The answer is environment variables.


What Are Environment Variables?

Environment variables are:

  • Key–value pairs

  • Defined by the operating system

  • Available to all processes running in a user’s environment

They are similar to variables in Bash scripts, but with one major difference:

Environment variables are available system-wide (within a user session), not just inside one script.

Naming Convention

By convention:

  • Environment variable names are UPPERCASE

  • Words are separated by underscores

Examples:

  • USER

  • HOME

  • SHELL

  • EDITOR

  • LANG


Common Environment Variables

Some important examples:

  • USER → current logged-in user

  • HOME → user’s home directory

  • SHELL → default shell program

  • LANG → language settings

A user can change many of these values, such as switching from Bash to Zsh.


Viewing Environment Variables

To display all environment variables:

printenv

To view them more conveniently:

printenv | less

To print a single variable:

printenv USER

To filter variables:

printenv | grep HOME

Using Environment Variables

Environment variables can be referenced using:

$VARIABLE_NAME

Example:

echo $HOME

They can be used:

  • In terminal commands

  • In Bash scripts

  • By applications and services


Why Create Custom Environment Variables?

One of the most important use cases is application configuration.

The Problem: Sensitive Data in Code

Applications often need:

  • Database usernames

  • Database passwords

  • API keys

  • Secret tokens

Hard-coding these values in source code is:

  • Insecure

  • Hard to change

  • Environment-specific


The Solution: Environment Variables

We store sensitive and configurable data in environment variables.

Examples:

  • DB_USERNAME

  • DB_PASSWORD

  • DB_NAME

  • DB_URL

Applications read these values from the environment instead of the code.

Every programming language supports reading environment variables.


Flexibility Across Environments

Applications usually run in multiple environments:

  • Development

  • Testing

  • Production

Each environment has:

  • Different databases

  • Different credentials

  • Different API keys

Using environment variables allows:

  • Same codebase

  • Different configurations

  • No code changes between environments


Creating Environment Variables (Temporary)

To create an environment variable in the current session:

export DB_USERNAME=myuser
export DB_PASSWORD=secret
export DB_NAME=mydb

Verify:

printenv | grep DB

Use them:

echo $DB_USERNAME

You can also change or remove them:

export DB_NAME=newdb
unset DB_PASSWORD

Important: Session Scope

Environment variables set using export:

  • Exist only in the current session

  • Disappear when the terminal is closed

This is a very important concept.


Making Environment Variables Permanent (User-Level)

To persist variables across sessions, add them to your shell’s RC file.

For Bash:

~/.bashrc

Example:

export DB_USERNAME=myuser
export DB_PASSWORD=secret
export DB_NAME=mydb

Apply changes:

source ~/.bashrc

Now:

  • Variables are available in all terminals

  • Variables persist after logout or reboot


System-Wide Environment Variables

There is also a global environment file:

/etc/environment

Variables defined here:

  • Apply to all users

  • Are system-wide


The PATH Variable (Critical Concept)

PATH is a global environment variable that contains:

  • A list of directories

  • Separated by colons (:)

  • Where executable programs are located

Example:

/usr/local/bin:/usr/bin:/bin

When you run:

ls

Linux searches each directory in PATH to find the ls binary.


Adding Your Own Command to PATH

Let’s create a custom executable.

Step 1: Create Script

vim welcome
#!/bin/bash
echo "Welcome to DevOps Bootcamp $USER"

Step 2: Make It Executable

chmod +x welcome

Adding Script Location to PATH (User-Level)

Edit ~/.bashrc:

PATH="$PATH:/home/nana"

Reload:

source ~/.bashrc

Now you can run:

welcome

From any directory.


Why PATH Is So Powerful

Thanks to PATH:

  • You don’t need full paths to commands

  • You can create your own commands

  • Applications become globally accessible

This is how system commands like java, python, and node work.


Summary

In this lecture, you learned:

  • What environment variables are

  • How user environments are isolated

  • How to view and use environment variables

  • Why applications rely on environment variables

  • How to create temporary and permanent variables

  • Difference between user-level and system-wide variables

  • How the PATH variable works

  • How to create your own executable commands

More from this blog

devops

22 posts