Managing Environment Variables (export, unset)
Learn to set, export, and unset environment variables, understanding their scope and impact on programs.
Managing Environment Variables (export, unset) is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Environment Variables?
Welcome! Today we'll explore environment variables, a core concept in Linux. Think of them as special values that your shell and programs can read to configure their behavior.
They're like global settings for your command-line environment.
Viewing Existing Variables
You already have many environment variables set! You can view them using the printenv or env commands. Let's see a few:
printenv | head -n 5Local vs. Environment Scope
Variables can have different scopes. By default, when you create a variable, it's local to your current shell session. This means other programs or new shells you open won't know about it.
An environment variable, however, is passed down to any child processes or subshells you start from your current shell.
Setting a Local Variable
To set a local variable, you simply assign a value to a name. Try this:
MY_LOCAL_VAR="Hello Coddy"
echo $MY_LOCAL_VAR
bash -c 'echo "Inside subshell: $MY_LOCAL_VAR"'Understanding 'export'
Notice how $MY_LOCAL_VAR wasn't visible in the subshell? This is where export comes in! The export command marks a local variable to be passed into the environment of child processes.
It essentially makes your variable 'global' for any new programs or shells launched from your current one.
Exporting a Variable in Action
Let's use export to make a variable available everywhere:
export MY_EXPORTED_VAR="CoddyKit"
echo "Original shell: $MY_EXPORTED_VAR"
bash -c 'echo "Inside subshell: $MY_EXPORTED_VAR"'How Programs Use Env Vars
Many programs and scripts use environment variables for configuration. For example:
PATH: Tells your shell where to look for executable commands.HOME: Stores the path to your home directory.LANG: Sets the language for programs.
They provide a flexible way to customize software behavior without changing code.
Removing Variables with 'unset'
To remove a variable, whether it's local or exported, you use the unset command. This clears its value and removes it from the shell's memory.
export TEMP_VAR="I will be gone"
echo "Before unset: $TEMP_VAR"
unset TEMP_VAR
echo "After unset: $TEMP_VAR"Common Use Cases
Environment variables are crucial for:
- Configuration: Setting API keys, database connections, or application modes.
- Paths: Extending your
PATHto include custom script directories. - User Settings: Defining default editors, mail programs, or shell prompts.
They make your shell and scripts more dynamic and adaptable.
Check Your Understanding
You've learned about setting and exporting variables. Consider the following scenario:
Lesson Summary
Great job! You've learned the fundamentals of managing environment variables:
- Local variables are confined to the current shell.
- The
exportcommand promotes a local variable to an environment variable, making it accessible to child processes. - The
unsetcommand removes a variable.
Understanding these concepts is key to configuring your Linux environment and writing flexible scripts. Next, we'll explore how to make these configurations permanent!
Frequently asked questions
Is the “Managing Environment Variables (export, unset)” lesson free?
Yes — the full text of “Managing Environment Variables (export, unset)” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Managing Environment Variables (export, unset)”?
Learn to set, export, and unset environment variables, understanding their scope and impact on programs. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Managing Environment Variables (export, unset)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Managing Environment Variables (export, unset)
- Shell Startup Files (.bashrc, .profile)
- Package Management (apt, yum, pacman)
- Understanding and Customizing the PATH Variable