Pass Environment Values to Commands

Pass Environment Values to Commands

A school script needs to know the current class, and you want to provide that setting without rewriting the script each time. An environment variable is a name-and-value setting passed to a child process when it starts. Programs can read it to select behavior or configuration.

In Bash, assigning a shell variable and exporting it are different steps. A value can exist in your current shell without being part of the environment given to another program. Let's cross that process boundary deliberately.

What you’ll learn

  • Set and export a value for a child process.
  • Distinguish a shell variable from an exported environment variable.

Export crosses a process boundary

  1. Parent shell

    Assignment creates a variable in this shell.

    SCHOOL_CLASS=10A
  2. Export

    export includes the value in the environment of subsequently launched children.

    export SCHOOL_CLASS
  3. Child process

    The child Bash can read its inherited environment value.

    10A
Separate local shell state from an exported child environment.

Export the class name to a child Bash process

Prepare the example

Use Bash for this example. No administrative account is required. The assignment and export must happen in the same parent shell before the child command starts. We use a fresh child Bash to make it clear which value was inherited.

Bash · Linux terminal

SCHOOL_CLASS=10A
export SCHOOL_CLASS
bash -c 'printf "%s\n" "$SCHOOL_CLASS"'

Expected result

10A

SCHOOL_CLASS=10A assigns a value in the current shell. There are no spaces around the assignment operator. export SCHOOL_CLASS marks it for inclusion in the environment of later child processes.

The last line launches another Bash and asks it to print the variable. Its command text is single-quoted so the parent passes that text literally; the child expands $SCHOOL_CLASS from its inherited environment. You should see 10A. This does not make the setting persistent across future independent logins, and a child changing its own environment does not automatically update the parent's value.

Temporary values and persistent startup configuration

Environment variables are named values inherited by programs started from a shell. They commonly hold paths, configuration choices and locations such as HOME or PATH.

What Are Environment Variables?

Environment variables are like global values that live inside your terminal session. They can store paths, settings, and preferences your shell and apps can use.

For example, the PATH variable tells Linux where to look for executable programs.

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Let’s say you want to add a new folder to your PATH or create your own variable like MY_NAME. That’s what we’ll learn next!

Step 1: Create a Temporary Variable

This variable works only for the current terminal session:

export MY_NAME="Alex"
echo $MY_NAME
Alex

But if you close this terminal, it’s gone. To make it permanent, you need to add it to a shell configuration file.

Step 2: Add Variables to .bashrc or .zshrc

If you're using the Bash shell (most common), open your ~/.bashrc file:

nano ~/.bashrc

Scroll to the bottom and add:

export MY_NAME="Alex"
export PATH="$PATH:/home/alex/my-tools/bin"

Save and exit (in nano, press Ctrl + X, then Y, then Enter).

Step 3: Apply the Changes

Now run:

source ~/.bashrc

This reloads your terminal configuration and applies the changes.

echo $MY_NAME
Alex

Step 4: Set Variables for All Users (Optional)

To set environment variables system-wide, edit /etc/environment (requires sudo):

sudo nano /etc/environment

Add variables like this (no export needed):

MY_NAME=Alex
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Save and restart your session for changes to take effect.

Place environment variables in the correct startup file

  • Use export VAR=value to define a variable temporarily.
  • Add export lines to ~/.bashrc or ~/.zshrc for permanent use.
  • Use /etc/environment for system-wide settings.

And that’s it! You’ve just learned how to configure environment variables confidently. These tricks come in super handy when setting up development tools, custom scripts, or just personalizing your terminal experience.

Try it, then check the worked answer

Your task: Make a shell value available to a child process.

Use Bash for this example. No administrative account is required. The assignment and export must happen in the same parent shell before the child command starts. We use a fresh child Bash to make it clear which value was inherited.

Bash · complete example for this question

SCHOOL_CLASS=10A
export SCHOOL_CLASS
bash -c 'printf "%s\n" "$SCHOOL_CLASS"'

Expected result

10A

Worked answer

export SCHOOL_CLASS

Use export to make the value available to a subsequently launched child. Printing a name with echo does not export it, and assignment alone is not generally sufficient. Think about the process receiving the value, not just whether the current shell can display it.

Check your decision

Make a shell value available to a child process.
Choose one answer. You can try again.

Answer and explanation

export SCHOOL_CLASS

Use export to make the value available to a subsequently launched child. Printing a name with echo does not export it, and assignment alone is not generally sufficient. Think about the process receiving the value, not just whether the current shell can display it.

A child receives exported values, not every shell variable

Separate local shell state from an exported child environment. Set the value before starting the child, quote expansions when using it, and configure persistence separately when needed. Do not put secrets into examples merely because environment variables are a convenient configuration mechanism.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Choose the Right Shell Startup File →