Tutorial

Read and Set Shell Variables

Mister P
by  Mister P · on
Linux
Practice reading built-in environment variables, creating your own shell variables, exporting them to child processes, and verifying inheritance — essential skills for the RHCSA EX200 exam.

# Read and Set Shell Variables

In this tutorial, you will read common environment variables, define your own shell variable, export it to child processes, and verify that child processes inherit the value you set. Along the way you will work with `echo`, `env`, `export`, and `bash`.

These skills appear directly on the RHCSA EX200 exam — understanding the difference between a shell variable and an exported environment variable is tested both in isolation and as a prerequisite for configuring services and scripts.

::remark-box
---
kind: info
---
**Before running any commands:** click the **+** button in the terminal tab bar to open a new terminal tab. The playground history tracking activates in new sessions only. Commands run in the original tab will not register for task verification.
::

---

## What You Will Build

By the end of this tutorial, the terminal will be in this state:

laborant@rocky-01 ~$ echo $MYGREETING Hello RHCSA laborant@rocky-01 ~$ bash -c 'echo $MYGREETING' Hello RHCSA


Both the parent shell and a child shell return the value you defined — this is environment inheritance in action.

---

## Step 1: Read Three Built-in Environment Variables

The shell provides a set of variables automatically. Use `echo` to read three of them.

```bash
echo $USER

Expected output:

laborant
echo $SHELL

Expected output:

/bin/bash
echo $PATH

Expected output (similar to):

/home/laborant/.local/bin:/home/laborant/bin:/usr/local/bin:/usr/bin:/bin

Each variable name is prefixed with $ — that is the signal to the shell to substitute the variable's value before the command runs.

Note

$USER, $SHELL, and $PATH are environment variables — they were exported by the login session and are visible to every command this shell launches.


Step 2: List All Environment Variables with env

Now use env to display every variable currently exported to the environment.

env

You will see many lines. Look for the variables you just read:

SHELL=/bin/bash
HOME=/home/laborant
USER=laborant
PATH=/home/laborant/.local/bin:/home/laborant/bin:/usr/local/bin:/usr/bin:/bin
LANG=en_US.UTF-8

Everything shown here is inherited by any command this shell launches.

I see too many lines — how do I find a specific variable?

Pipe env through grep:

env | grep PATH

This filters the output to lines that contain PATH.


Step 3: Create a Shell Variable

Define your own variable.

MYGREETING="Hello RHCSA"

No output is produced — the shell accepts the assignment silently. Confirm the value is set:

echo $MYGREETING

Expected output:

Hello RHCSA
Important

MYGREETING is now a shell variable — it lives only in this shell session. It has not been exported, so a child process cannot see it yet.


Step 4: Confirm the Variable Is Not Yet Inherited

Check what a child shell sees before you export MYGREETING.

bash -c 'echo $MYGREETING'

Expected output:

The output is an empty line. The child shell has no knowledge of MYGREETING because it has not been exported yet.

Why does the child shell print nothing?

When bash starts a child process, it only passes exported variables into that child's environment. A plain assignment like MYGREETING="Hello RHCSA" marks the variable as local to the current shell. The child shell receives no value for MYGREETING, so $MYGREETING expands to an empty string.


Step 5: Export the Variable

Mark MYGREETING for inclusion in the environment of every child process this shell launches.

export MYGREETING

Again, no output — the shell accepts the export silently.

Note

You can combine assignment and export in a single step: export MYGREETING="Hello RHCSA". Both forms are valid and appear on the exam.


Step 6: Verify Inheritance in a Child Process

Now that MYGREETING is exported, confirm that a child shell inherits it.

bash -c 'echo $MYGREETING'

Expected output:

Hello RHCSA

The same value you set in the parent shell is now visible inside the child shell — this is environment inheritance.


Step 7: Confirm the Complete Final State

Verify both the parent shell and a child shell return the expected value.

echo $MYGREETING

Expected output:

Hello RHCSA
bash -c 'echo $MYGREETING'

Expected output:

Hello RHCSA

Both commands return Hello RHCSA — the system is in the state described at the top of this tutorial.

Note

Exam tip: The export does not propagate upward. If you start a child shell, set a variable there, and export it, the parent shell will never see it. Variables flow down to children, never up to parents.


What You Accomplished

In this tutorial, you:

  1. Read the values of built-in environment variables $USER, $SHELL, and $PATH using echo
  2. Listed all exported environment variables using env
  3. Defined a new shell variable MYGREETING with an assigned value
  4. Confirmed that an unexported variable is invisible to child processes
  5. Exported MYGREETING using export
  6. Verified that a child process launched with bash -c inherits the exported variable
Why does export work this way?

Unix processes are created with fork() — the child receives a copy of the parent's environment at the moment of creation. The operating system copies only the exported variables table into the new process image. Changes made in the child after fork do not affect the parent, and variables that were never exported are never included in the copy.

This is why login shell configuration files (~/.bash_profile, /etc/profile) use export — any variable they want subshells and programs to see must be explicitly placed in the environment.


Next Steps

  • Persist variables across sessions — write export MYGREETING="Hello RHCSA" into ~/.bash_profile and verify it survives logout
  • Unset a variable — try unset MYGREETING and confirm echo $MYGREETING returns an empty line
  • Inspect a variable's export status — run declare -p MYGREETING before and after export and compare the flags shown

About the Author

Mister P

Mister P

Find this author online

More tutorials you might like

Learn by doing, not just by reading or watching

Sign up for a free account to start a VM playground right on this page, track your progress, and get notified about new learning materials.

Sign up for free