Introduction
In previous labs, we looked at existing variables like User. Now, let's create your own "sticky notes".
A variable is just a name that holds some text.
However, when you create a variable, it is private to your current shell. To share it with other programs, you must "export" it.
That distinction between a shell variable and an environment variable is one of the central ideas of this module. Not every variable automatically becomes part of the environment seen by child processes.
In this lab, you will:
- Create a variable
MYCAFE. - Read its content.
- "Export" it.
- Verify it works in a second shell.
Create a Variable
Let's create a variable called MYCAFE and set it to "espresso".
Creating a variable is like writing on a sticky note.
Run this:
MYCAFE="espresso"
Important: Do not use spaces around the = sign.
- Correct:
MYCAFE="espresso" - Wrong:
MYCAFE = "espresso"
If you use spaces, the shell thinks MYCAFE is a command.
This is a good example of how shell syntax is sensitive to whitespace. In the shell, spaces are not just visual formatting; they often separate words and arguments with real meaning.
Print Variable
Now, verify the value is stored.
To access the value, use a dollar sign $.
Run this:
echo $MYCAFE
Output:
espresso
If you omit the $, echo just prints the word "MYCAFE".
Export Variable
Right now, MYCAFE is a local variable. It's like a note in your private notebook.
To share it with other programs, we must export it. This turns it into an environment variable (like posting it on a bulletin board).
You can think of export as telling the shell, "Include this variable when you launch child programs." Without export, the value stays local to the current shell only.
Run this:
export MYCAFE
Now, MYCAFE is passed down to any program you start from this shell.
Test in Child Shell
Let's prove it works by starting a "child" shell.
- Start a new
bashsession:
bash
You are now inside a new shell process.
- Check if it knows
MYCAFE:
echo $MYCAFE
Output:
espresso
It works! The child shell inherited the value.
Inheritance is the key word here. Child processes often begin with a copy of the parent's environment, which is why exported variables are such a common way to pass context to scripts and commands.
- Exit the child shell and return to the parent:
exit
Summary
In this lab, you practiced the lifecycle of a variable:
- Creation:
MYCAFE="espresso"(No spaces!) - Reading:
echo $MYCAFE(Don't forget the$) - Sharing:
export MYCAFE(Making it public to child processes)
This mechanism is exactly how the system sets up your environment (PATH, USER, etc.) before you even log in!



