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:
|
|
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 usernamePATH
- Where the system looks for programsPWD
- Your current directory
To see just one variable, use echo
with a $
sign:
Creating Your First Environment Variable
Let’s create a simple environment variable. In your terminal, type:
|
|
Now check it worked:
|
|
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:
|
|
If it doesn’t exist, create it:
|
|
Now edit it with a simple text editor:
|
|
Add this line at the end:
|
|
Save the file (in nano, press Ctrl+X, then Y, then Enter).
Now, to make this take effect in your current terminal:
|
|
Test it:
|
|
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
|
|
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
Adding Programs to Your PATH
Sometimes you install programs that aren’t automatically available. You can add their location to your 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):
For Production (on the server):
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
Don’t Forget the export Command
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:
|
|
Testing Your Setup
After making changes to your profile, always test them:
- Open a new terminal window
- Check that your variables are set:
echo $MY_VARIABLE
- Try using any aliases you created
- 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
Development Environment
Docker Shortcuts
AWS CLI Setup
Kubernetes Shortcuts
Git Shortcuts
System Information
Copy and paste any of these snippets into your ~/.bashrc file, then run source ~/.bashrc
to start using them!