← Back to Bash Course | Chapter 2: Variables & Types | Lesson 6 of 13

Environment Variables & export

An environment variable is a regular variable that has been marked so that it is also visible to any program the script launches, not just the script itself.

export Makes a Variable Inheritable

By default, a variable assigned in a script is only visible within that script's own shell, not to any programs it launches. export flags the variable so that it becomes part of the environment handed to every child process the script starts from that point on.

Example: export Makes a Variable Inheritable

bash
#!/bin/bash
GREETING="hello from parent"
export GREETING
bash -c 'echo "Child sees: $GREETING"'

Unexported Variables Stay Local

A variable that is never exported is only visible in the current shell and any subshells that inherit its scope directly -- it will not be visible in a separately invoked child process. This distinction is easy to miss because both kinds of variables look identical when you just echo them in the same script.

Warning: The child process prints an empty value for LOCAL_ONLY because it was never exported.

Example: Unexported Variables Stay Local

bash
#!/bin/bash
LOCAL_ONLY="not exported"
bash -c 'echo "Child sees LOCAL_ONLY as: [$LOCAL_ONLY]"'

Common Built-in Environment Variables

Bash and the operating system pre-populate several environment variables at login, such as HOME (the current user's home directory), USER (username), and PATH (the list of directories searched for commands). Scripts frequently read these rather than hard-coding values that differ between systems.

Example: Common Built-in Environment Variables

bash
#!/bin/bash
echo "Home directory: $HOME"
echo "Current user: $USER"
echo "First PATH entry: $(echo "$PATH" | cut -d: -f1)"

Safely Extending PATH

When adding a directory to PATH, always include the existing value ($PATH) in the new assignment, otherwise you overwrite it entirely and the shell can no longer find standard commands. Appending or prepending with a colon separator keeps all previous search locations intact.

Example: Safely Extending PATH

bash
#!/bin/bash
export PATH="/usr/local/mytools:$PATH"
echo "$PATH" | tr ':' '\n' | head -n 3
Common Mistakes
  1. Assuming every shell variable is automatically available to child processes/scripts you call; by default variables are local to the current shell and must be exported to be inherited.
  2. Thinking export copies the variable to the child permanently in both directions; changes a child process makes to an exported variable never flow back up to the parent shell.
  3. Modifying well-known environment variables like PATH without saving the old value first, e.g. PATH=/usr/local/bin instead of PATH=/usr/local/bin:$PATH, which can wipe out the ability to find other commands.
Chapter Summary
  • export VAR=value (or export VAR after a plain assignment) makes a variable part of the environment passed to child processes.
  • Environment variables are inherited by child processes at the moment they are started, and changes in the child do not propagate back to the parent.
  • Common built-in environment variables include PATH, HOME, USER, and PWD.
  • env or printenv lists all currently exported environment variables.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.