keepcalculator

06 Numbers and Powers

Bash Export Variable: Syntax, Scope & Examples

Card No. 06·61835 · Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered
You enter
Finish time, Distance
You get
Average pace, Average speed
Rounding
2 dp

To make a variable available to child processes in Bash, you use the export command. Exporting a variable marks it for inheritance: any command or script you launch from that shell will see the variable’s value. You can export a new variable directly (export MY_VAR=value) or export an existing one (export MY_VAR).

Formula

The calculator above is a pace calculator, not a Bash tool, but it uses the same kind of arithmetic you might script in Bash. The formula for pace is:

pace = time / distance

where time is in minutes and distance is in kilometers. The speed is:

speed = distance / (time / 60)

which converts minutes to hours. In a Bash script, you would store these values in variables, and if you wanted a subprocess to read them, you would export them.

Worked Example

Using the default values from the calculator: time = 50 minutes, distance = 10 km.

  1. Pace = 50 / 10 = 5.00 min/km
  2. Speed = 10 / (50 / 60) = 10 / 0.8333 = 12.00 km/h

The calculator shows 5.00 min/km and 12.00 km/h, matching these results.

What Does export Do in Bash?

In Bash, every variable you create is local to the current shell by default. When you run a command or a script, Bash forks a child process. That child gets a copy of the shell’s environment, but only the variables that were marked for export are included. export adds the variable to the environment, so the child process can read it.

Without export, the variable exists only in the current shell. This is a common source of confusion: a script you run from the terminal won’t see a variable you set unless you export it first.

How to Export a Variable

The syntax is straightforward. You have three common forms:

  • export VAR=value – creates and exports in one step
  • VAR=value; export VAR – sets first, then exports
  • export VAR – exports an existing variable

You can also export multiple variables at once: export VAR1 VAR2. To see all exported variables, type env or export -p. To remove an export (but keep the variable), use export -n VAR. To unset the variable entirely, use unset VAR.

Exporting in a Script vs. Interactive Shell

When you export a variable in an interactive terminal, it affects every command you run from that terminal. When you export inside a script, it affects only the commands that script runs. The script’s exports do not propagate back to your terminal – each script runs in its own child shell.

If you want a variable to be available in every new terminal, you must add the export line to a startup file like ~/.bashrc or ~/.profile. That file is sourced when the shell starts, so the export happens automatically.

Common Mistakes and Pitfalls

One frequent mistake is putting spaces around the equals sign: export VAR = value is wrong. Bash treats VAR as a command name. The correct form has no spaces.

Another issue is exporting a variable with a value containing spaces. You must quote it: export GREETING="Hello World". Without quotes, Bash splits the value into multiple words.

A third mistake is expecting a child process to change the parent’s variable. A child process gets a copy, so changes it makes are lost when it exits. To change the parent, you need to source a script (source script.sh) rather than run it.

export vs. env vs. set

These three commands are often confused. export marks a variable for inheritance. env runs a command with a modified environment, or prints the current environment. set shows all shell variables (including non-exported ones) and also controls shell options.

In practice, you use export to share variables, env to run a one-off command with a temporary variable, and set to debug or inspect. For example, env VAR=value command runs command with VAR set, but does not change your current shell.

Real-World Use Cases

Exporting is essential for configuration. Common examples include:

  • Setting PATH to include custom script directories
  • Storing database connection strings for an application
  • Passing API keys to a deployment script
  • Setting language or locale variables like LANG

In a CI/CD pipeline, you often export build variables so that subsequent steps can read them. In a Docker container, environment variables are passed via -e flags, which are essentially exported variables.

Frequently Asked Questions

How do I export a variable permanently?

Add the export line to your shell’s startup file. For Bash, that is usually ~/.bashrc for interactive shells and ~/.profile for login shells. After editing, run source ~/.bashrc to apply changes in the current terminal.

Why doesn’t my script see the variable I exported?

If you exported the variable in your terminal, but the script still doesn’t see it, check that you exported it in the same shell that runs the script. Also verify the variable name is spelled exactly the same, including case. If the script is run with sudo, the environment is often reset – use sudo -E to preserve it.

Can I export a variable and use it in the same command?

Yes. For example, export FOO=bar && echo $FOO works because the export happens before the echo. However, FOO=bar command only sets the variable for that single command, not for the shell.

What is the difference between export and declare -x?

They are equivalent. declare -x VAR=value is the same as export VAR=value. The declare form is useful when you also want to set attributes like integer or readonly.

How do I unexport a variable without deleting it?

Use export -n VAR. This removes the export attribute, so the variable remains in the shell but is no longer passed to child processes. To delete it entirely, use unset VAR.

Does export work in all shells?

The export command exists in POSIX shells like Bash, Zsh, and Dash. In fish shell, you use set -x instead. The concept is the same, but syntax differs.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0 | 100000
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →