When to use export in Bash

Bjorn Krolsavatar

Bjorn Krols

Published on
07 June 2021

Today we will explore the difference between

NAME=value

and

export NAME=value

Both snippets define variables but...

Without export, the shell variable scope is restricted to the shell, remaining available to any other process. Use NAME=value for temporary or loop variables private to the current shell process.

export makes the shell variable available to child processes via the environment, turning it into an environment variable. Use export NAME=value for settings and variables that have meaning to a sub-process.

When you want to make the value of a variable accessible in subshells, the shell's export command should be used.

# Create a shell variable
AGE=30

# Turn the shell variable into an environment variable
export AGE

Subscribe to our newsletter

The latest news, articles, and resources, sent to your inbox weekly.

More like this