Environment Variables and Profiles: A Beginner’s Guide for DevOps

If you’re starting your DevOps journey, you’ll constantly hear about “environment variables” and “shell profiles.” Don’t worry - they’re simpler than they sound, but incredibly powerful once you understand them. Let’s break this down step by step.

What Are Environment Variables?

Think of environment variables as sticky notes that your computer uses to remember important information. Just like you might write your WiFi password on a sticky note, your system uses environment variables to store information that programs need to work properly.

For example, when you type a command like ls or docker, your system knows where to find these programs because their locations are stored in an environment variable called PATH.

Why DevOps Engineers Need Them

As a DevOps engineer, you’ll work with different environments:

  • Development (where you test new code)
  • Staging (where you test before going live)
  • Production (the live system users see)

Each environment needs different settings - different database passwords, different server addresses, different API keys. Environment variables let you switch between these settings easily without changing your code.

Your First Look at Environment Variables

Let’s see what environment variables are already on your system. Open your terminal and type:

1
env

You’ll see a long list that might look overwhelming. Don’t worry! Let’s look at a few important ones:

  • HOME - Your home directory (like /home/john)
  • USER - Your username
  • PATH - Where the system looks for programs
  • PWD - Your current directory

To see just one variable, use echo with a $ sign:

1
2
echo $HOME
echo $USER

Creating Your First Environment Variable

Let’s create a simple environment variable. In your terminal, type:

1
export MY_NAME="John"

Now check it worked:

1
echo $MY_NAME

Congratulations! You just created your first environment variable. The word export means “make this available to other programs too.”

But here’s the catch - this variable only exists until you close your terminal. When you open a new terminal, it’s gone. That’s where profiles come in.

What Are Shell Profiles?

Shell profiles are like startup scripts for your terminal. Every time you open a new terminal, these files run automatically and set up your environment just the way you like it.

Think of it like your morning routine - you do the same things every day to get ready. Shell profiles do the same thing for your terminal.

The Main Profile Files You Need to Know

There are several profile files, but as a beginner, focus on these two:

~/.bashrc

This is your main configuration file. The ~ means your home directory, and the dot (.) means it’s a hidden file. This file runs every time you open a new terminal window.

~/.bash_profile

This file runs when you first log into your system (like when you SSH into a server). It usually just loads your ~/.bashrc file.

Making Variables Permanent

Let’s make that MY_NAME variable permanent. We’ll add it to your ~/.bashrc file.

First, let’s see if the file exists:

1
ls -la ~/.bashrc

If it doesn’t exist, create it:

1
touch ~/.bashrc

Now edit it with a simple text editor:

1
nano ~/.bashrc

Add this line at the end:

1
export MY_NAME="John"

Save the file (in nano, press Ctrl+X, then Y, then Enter).

Now, to make this take effect in your current terminal:

1
source ~/.bashrc

Test it:

1
echo $MY_NAME

Great! Now every time you open a new terminal, this variable will be there.

Real DevOps Examples

Let’s look at some practical examples that DevOps engineers actually use:

Setting Your Default Text Editor

1
export EDITOR="nano"

Now when programs need you to edit something, they’ll use nano instead of the default (often vim, which can be confusing for beginners).

Setting Up Your Development Environment

1
2
3
export NODE_ENV="development"
export DATABASE_URL="localhost:5432"
export API_KEY="your-dev-api-key"

Adding Programs to Your PATH

Sometimes you install programs that aren’t automatically available. You can add their location to your PATH:

1
export PATH="$HOME/my-programs:$PATH"

This adds your personal programs folder to the beginning of the PATH.

A Simple DevOps Workflow

Here’s a real scenario: You’re working on a web application that needs different settings for development and production.

Instead of changing code every time, you can use environment variables:

For Development (in ~/.bashrc):

1
2
3
export APP_ENV="development"
export DATABASE_URL="localhost:5432/myapp_dev"
export DEBUG="true"

For Production (on the server):

1
2
3
export APP_ENV="production"  
export DATABASE_URL="prod-server:5432/myapp"
export DEBUG="false"

Your application code can then check these variables and behave differently in each environment.

Common Mistakes to Avoid

Don’t Put Spaces Around the = Sign

1
2
3
4
5
# Wrong
export MY_VAR = "value"

# Right  
export MY_VAR="value"

Don’t Forget the export Command

1
2
3
4
5
# This only works in the current shell
MY_VAR="value"

# This makes it available to other programs
export MY_VAR="value"

Be Careful with Sensitive Information

Never put passwords or secret keys directly in your profile files, especially if you share your configuration with others or store it in version control.

Organizing Your Profile

As you add more variables, your ~/.bashrc can get messy. Here’s a simple way to organize it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ~/.bashrc

# ========================================
# Personal Settings
# ========================================
export EDITOR="nano"
export BROWSER="firefox"

# ========================================  
# Development Environment
# ========================================
export NODE_ENV="development"
export GOPATH="$HOME/go"

# ========================================
# Custom PATH additions
# ========================================
export PATH="$HOME/bin:$PATH"

# ========================================
# Useful Aliases (shortcuts)
# ========================================
alias ll="ls -la"
alias ..="cd .."
alias grep="grep --color=auto"

Testing Your Setup

After making changes to your profile, always test them:

  1. Open a new terminal window
  2. Check that your variables are set: echo $MY_VARIABLE
  3. Try using any aliases you created
  4. Make sure you didn’t break anything

If something goes wrong, you can always edit your ~/.bashrc file again to fix it.

Next Steps

Once you’re comfortable with basic environment variables and profiles, you can explore:

  • Creating functions in your profile for common tasks
  • Using different profiles for different projects
  • Learning about other shells like zsh
  • Integrating with tools like Docker and Kubernetes

Quick Reference

View all variables: env View one variable: echo $VARIABLE_NAME Set temporary variable: export VAR="value" Edit your profile: nano ~/.bashrc Reload your profile: source ~/.bashrc Remove a variable: unset VARIABLE_NAME


Ready-to-Use Code Snippets

Here are some practical snippets you can add to your ~/.bashrc file:

Basic DevOps Setup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Text editor and browser
export EDITOR="nano"
export BROWSER="firefox"

# Useful aliases
alias ll="ls -la"
alias ..="cd .."
alias ...="cd ../.."
alias h="history"
alias c="clear"

Development Environment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Node.js development
export NODE_ENV="development" 
export PATH="$HOME/.npm-global/bin:$PATH"

# Python development  
export PYTHONPATH="$HOME/python-projects:$PYTHONPATH"

# Go development
export GOPATH="$HOME/go"
export PATH="$GOPATH/bin:$PATH"

Docker Shortcuts

1
2
3
4
5
6
7
8
# Docker aliases
alias d="docker"
alias dc="docker-compose"
alias dps="docker ps"
alias di="docker images"

# Docker cleanup
alias docker-clean="docker system prune -f"

AWS CLI Setup

1
2
3
4
5
6
7
# AWS configuration
export AWS_DEFAULT_REGION="us-west-2"
export AWS_OUTPUT="json"

# AWS profile switching
alias aws-dev="export AWS_PROFILE=development"
alias aws-prod="export AWS_PROFILE=production"

Kubernetes Shortcuts

1
2
3
4
5
6
7
8
9
# Kubernetes aliases
alias k="kubectl"
alias kgp="kubectl get pods"
alias kgs="kubectl get services" 
alias kgd="kubectl get deployments"

# Quick context switching
alias k-dev="kubectl config use-context development"
alias k-prod="kubectl config use-context production"

Git Shortcuts

1
2
3
4
5
6
7
# Git aliases
alias g="git"
alias gs="git status"
alias ga="git add"
alias gc="git commit"
alias gp="git push"
alias gl="git pull"

System Information

1
2
3
4
5
# System monitoring shortcuts
alias df="df -h"      # Disk usage in human readable format
alias du="du -h"      # Directory usage in human readable format  
alias ps="ps aux"     # Show all processes
alias ports="netstat -tuln"  # Show open ports

Copy and paste any of these snippets into your ~/.bashrc file, then run source ~/.bashrc to start using them!