Back to blog
GuidesAugust 30, 2026·11 min read

Check Environment Variables Linux: How to Set, List & Manage

Check Environment Variables Linux: How to Set, List & Manage

Check Environment Variables Linux: How to Set, List & Manage

Need to check environment variables linux? Be it troubleshooting a deployment, configuring a new server or setting up a development environment - learning to list, set and manage environment variables on Linux systems is a must-have skillset. You will find all the Linux commands necessary to check environment variables linux systems expose here.

Learning about environment variables on Linux gives you access to a whole configuration layer that governs command lookup, application behaviour and much more. Knowing how to manipulate environment variables can save you a lot of trouble debugging.


1: Environment Variables and Their Meaning for Sysadmins and Developers

Environment variables are dynamic named values that modify the behaviour of processes in a Linux system. You can think of them as of a kind of configuration options for OS or applications.

Key properties:

  • Dynamic values that affect processes behavior

  • Key-value pairs: VARIABLE_NAME=value

  • Inherited by child processes: when you launch a process, it inherits the environment of the parent process

  • Case-sensitive: HOME and home are two different variables

  • String-based: values are always stored as text

And why do they matter for developers and sysadmins?

Table

Use case

Example Variable

User home directory

HOME=/home/john

Default shell

SHELL=/bin/bash

Search path for executables

PATH=/usr/local/bin:/usr/bin

Language and locale

LANG=en_US.UTF-8

Application configuration

AWS_REGION=us-east-1

Secret management

DATABASE_PASSWORD=secret

Without environment variables you would always need to provide absolute paths, applications would not know where the user's files are located and system-wide configuration would be impossible.


2: Listing Environment Variables: env, printenv and set

There are several ways to quickly check environment variables linux exposes. Depending on your needs you will use each command below.

2.1 env: Run Program in Modified Environment

This Linux command prints all exported environment variables for the current session.

bash

env

Sample output:

bash

SHELL=/bin/bash
PWD=/home/devuser
LOGNAME=devuser
HOME=/home/devuser
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
USER=devuser

Pro tip: env can also be used to run commands in modified or clean environment:

bash

env -i bash          # Start shell with NO environment variables
env VAR=value ./app  # Run app with VAR set temporarily

2.2 printenv: List All or Specific Environment Variables

printenv command prints all or specific Linux environment variables (based on the argument you specify). The easiest way to linux print environment variables and linux display environment variable values.

bash

printenv

Key difference from env: printenv accepts parameters to show specific variables (covered in 3).

2.3 set: Show All Variables (including shell variables)

set command prints all variables: environment variables, shell variables, shell functions and shell aliases.

bash

set

Output will be larger as it contains:

  • Exported environment variables

  • Local shell variables (non-exported)

  • Functions and aliases

bash

# Count the difference
printenv | wc -l    # Typically 30-50 lines
set | wc -l         # Typically 100+ lines

When to use each one:

Table

Command

Prints

Best for

env

Exported environment variables

Running programs with modified environment

printenv

Exported environment variables

Quickly listing all env variables

set

All shell variables + functions

Debugging shell scripts


3: Viewing Specific Variables -- echo $VAR and printenv VAR

When you need to linux show environment variables individually, use these approaches.

3.1 echo $VARIABLE_NAME

The easiest way to linux view environment variables individually:

bash

echo $HOME
# Output: /home/devuser

echo $PATH
# Output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

echo $USER
# Output: devuser

Key usage rules:

  • Always use $ to dereference the variable

  • Quote the variable to avoid problems with spaces: echo "$HOME"

  • Use curly braces to concatenate: echo ${HOME}/documents

bash

# Without braces -- will not work
echo $HOME documents    # /home/devuser documents

# With braces -- clear boundaries
echo ${HOME}/documents  # /home/devuser/documents

3.2 printenv VARIABLE_NAME

More robust approach that will not have problems with shell interpretations.

bash

printenv HOME
# Output: /home/devuser

printenv PATH
# Output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

printenv vs echo comparison:

Table

Scenario

echo

printenv

Simple value

Works

Works

Value starts with dash

Interpreted as option

Safe

Value contains newlines

Can behave unexpectedly

Prints as-is

Value for non-existing variable

Prints empty line

Exits with code 1

bash

# printenv will tell you if the variable is not defined
printenv NONEXISTENT
# (no output, exit code 1)

echo $?
# Output: 1

3.3 var check: Quick Verification Patterns

Quick patterns for checking environment variables in linux during troubleshooting:

bash

# Is variable defined? (not empty)
[ -n "$AWS_REGION" ] && echo "AWS_REGION is set to $AWS_REGION" || echo "AWS_REGION is not set"

# Is variable defined? (even if empty)
[ -v AWS_REGION ] && echo "AWS_REGION exists" || echo "AWS_REGION does not exist"

# Define value for unexisting one
echo "${DATABASE_URL:-postgresql://localhost:5432/mydb}"

4: Setting Temporary vs Permanent Variables

It is important to know the scope and persistence when you set system variable linux configurations.

4.1 Temporary Variables (current shell only)

If the variable is set without export command, it will be shell-only -- cannot be seen by child processes.

bash

MY_VAR="hello world"
echo $MY_VAR          # Works: hello world
bash -c 'echo $MY_VAR' # Will not work: empty

And also a pattern to check any variable name:

bash

VARNAME="test_value"
echo $VARNAME         # Output: test_value

4.2 Export Variables: Session-Scope (available to child processes)

To make the variable available to the child processes use export command:

bash

export API_KEY="sk-live-abc123xyz"
bash -c 'echo $API_KEY'  # Now works: sk-live-abc123xyz

Oneliner syntax:

bash

export NODE_ENV=production

Also combine assignment and export commands:

bash

export LOG_LEVEL=debug

They will be valid until you terminate the terminal session.

4.3 User-Scope: Permanent Variables (.bashrc, .bash_profile, .zshrc)

To persist the variables across terminal sessions, add them to startup file of your shell.

For Bash users:

bash

# Edit ~/.bashrc for interactive shells
echo 'export EDITOR=vim' >> ~/.bashrc
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc

# Apply changes
source ~/.bashrc

For Zsh users:

bash

echo 'export EDITOR=vim' >> ~/.zshrc
source ~/.zshrc

Which file to edit?

Table

File

When it runs

Best for

~/.bashrc

Every interactive shell

Aliases, functions, env variables

~/.bash_profile

Only login shells

PATH, environment setup

~/.profile

Login shells (generic)

Cross-shell compatibility

4.4 System-Wide Permanent Variables (/etc/environment, /etc/profile)

For linux system variables that are visible to all users:

/etc/environment: key-value pairs, no shell syntax:

bash

# Edit with sudo
sudo nano /etc/environment

# Contents:
JAVA_HOME=/usr/lib/jvm/java-17-openjdk
M2_HOME=/opt/maven

/etc/profile.d/: shell scripts that execute for all users:

bash

sudo nano /etc/profile.d/custom-env.sh

# Contents:
export COMPANY_DOMAIN="example.com"
export DEFAULT_REGION="us-east-1"

/etc/profile: system-wide environment setup for login shells:

bash

sudo nano /etc/profile

Security warning: Never store secrets (passwords, API keys) in system-wide files that can be read by all users. Use secrets management tools instead.


5: Managing PATH and System-Wide Variables

The PATH environment variable is the most frequently modified variable. It controls search path for executables in the shell.

5.1 Understanding PATH

bash

echo $PATH
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The shell searches directories from left to right. First match wins.

5.2 Append Variables to PATH

Wrong way (will result in duplicates or mess with PATH):

bash

export PATH=$PATH:/new/path

Correct way (idempotent, no duplicates):

bash

# Add to beginning (priority: high)
export PATH="/usr/local/go/bin:$PATH"

# Add to end (priority: low)
export PATH="$PATH:$HOME/.local/bin"

# In ~/.bashrc use a check
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
    export PATH="$HOME/.local/bin:$PATH"
fi

5.3 Real-life PATH examples

Go developer:

bash

export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Node.js developer (with nvm installed):

bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Python developer:

bash

export PATH="$HOME/.local/bin:$PATH"
export PYTHONDONTWRITEBYTECODE=1
export PYTHONUNBUFFERED=1

5.4 Unsetting Variables

bash

# Unset the variable (it will be removed)
unset TEMP_VAR

# Verify it's been removed
printenv TEMP_VAR    # Exit code 1

6: Real-life examples for Developers and SysAdmins

Example 1: Database connection in a development environment

bash

# ~/.bashrc or ~/.bash_profile
export DB_HOST=localhost
export DB_PORT=5432
export DB_NAME=myapp_dev
export DB_USER=devuser
export DB_PASSWORD="$(cat ~/.secrets/db_password.txt)"  # Read from file, not hardcoded

Example 2: Cloud provider CLI configuration

bash

# AWS
export AWS_PROFILE=production
export AWS_REGION=ap-northeast-1
export AWS_DEFAULT_OUTPUT=json

# GCP
export GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/gcloud/service-account.json"

# Azure
export AZURE_SUBSCRIPTION_ID=xxxx-xxxx-xxxx

Example 3: Container development (Docker)

bash

# ~/.bashrc
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
export REGISTRY=registry.example.com

Example 4: CI/CD pipeline variables

bash

#!/bin/bash
# deploy.sh -- script used in CI/CD

set -euo pipefail

# Require variables to be defined
: "${DEPLOY_ENV:?Need to set DEPLOY_ENV}"
: "${VERSION:?Need to set VERSION}"
: "${API_TOKEN:?Need to set API_TOKEN}"

echo "Deploying version $VERSION to $DEPLOY_ENV..."

Example 5: Sysadmin: Managing Java versions

bash

# /etc/profile.d/java.sh
export JAVA_8_HOME=/usr/lib/jvm/java-8-openjdk
export JAVA_11_HOME=/usr/lib/jvm/java-11-openjdk
export JAVA_17_HOME=/usr/lib/jvm/java-17-openjdk

# Use default Java 17
export JAVA_HOME=$JAVA_17_HOME
export PATH=$JAVA_HOME/bin:$PATH

# Also Java developers need CLASSPATH variable to locate classes and jar files
export CLASSPATH=".:$HOME/lib/*"

# Switch versions helper function
use_java() {
    local version=$1
    export JAVA_HOME=$(eval echo "\$JAVA_${version}_HOME")
    export PATH=$(echo $PATH | tr ':' '\n' | grep -v 'jvm' | tr '\n' ':')$JAVA_HOME/bin
    java -version
}

Example 6: Debugging Environment Problems

bash

# Is a variable inherited via SSH?
ssh server 'printenv | grep MY_VAR'

# Compare environments of two users
sudo -u www-data printenv | sort > /tmp/www-env
printenv | sort > /tmp/my-env
diff /tmp/www-env /tmp/my-env

# Trace what a command sees
curl -s https://httpbin.org/get | jq '.headers'
# Or locally:
env -i HOME=$HOME PATH=$PATH bash -c 'printenv'

Example 7: .env File for Application

Many applications use .env files to load variables. They can be loaded with:

bash

# Manually
set -a  # Automatically export all variables
source .env
set +a

# Or use direnv
# .envrc file in the project directory:
export $(grep -v '^#' .env | xargs)

Sample .env file:

bash

APP_ENV=development
DEBUG=true
DATABASE_URL=postgresql://localhost:5432/myapp
REDIS_URL=redis://localhost:6379
SECRET_KEY=dev-secret-do-not-use-in-production

7: Environment Variables in Windows vs Linux

If you have experience with Windows, concepts are the same but commands are different. Here is how to check environment variables linux compared to Windows:

Table

Task

Linux

Windows (CMD)

Windows (PowerShell)

List all variables

env or printenv

set

Get-ChildItem Env:

Show one variable

echo $VAR

echo %VAR%

Get started with Orbit Servers

Low-latency VPS, bare metal, and colocation across the US, EU, and APAC - provisioned instantly and built for performance-critical workloads.

Get started
J

Written by

Julius

Related posts