Exporting Variables with export
The export built-in command marks a variable so that Bash passes it, with its value, to every program it starts afterward. Exporting is what turns an ordinary shell variable into an environment variable.
Syntax
export has several forms:
export NAME # export a variable that already exists
export NAME=value # assign and export in one step
export A=1 B=2 # several at once
export -p # list exported variables
export -n NAME # remove the export attribute
export -f function_name # export a function
declare -x NAME=value has the same effect as export NAME=value. The -x stands for "export".
Assigning and Exporting in One Step
The most common form assigns a value and exports the variable in the same command:
export V2=assigned_and_exported
declare -p V2
declare -x V2="assigned_and_exported"
The declare -p output confirms the -x attribute. Notice that there are no spaces around the equals sign, which is required in every Bash assignment. Quote the value if it contains spaces:
export TITLE="My Big Report"
bash -c 'echo "child TITLE=[$TITLE]"'
child TITLE=[My Big Report]
Exporting an Existing Variable
You can assign a variable first and export it later. Compare the attributes before and after:
V1=plain
declare -p V1
export V1
declare -p V1
declare -- V1="plain"
declare -x V1="plain"
The variable keeps its value, and only its attribute changes.
Exporting Marks the Variable, Not the Value
When you export a variable, Bash does not freeze its value. The variable carries the export attribute from then on, and each program you start receives whatever the variable holds at that moment. A later assignment, even one without export, keeps the attribute:
export NUM=1
NUM=2
bash -c 'echo "child NUM=$NUM"'
child NUM=2
The second line assigned 2 with no export in front, yet the child received 2. You only need to export a name once.
Exporting Before a Variable Has a Value
export NAME also works on a name that has not been assigned yet. Bash remembers the attribute, but the variable has no value, so it does not appear in the environment until it gets one:
unset NEWV
export NEWV
declare -p NEWV
printenv NEWV
echo "printenv status=$?"
declare -x NEWV
printenv status=1
declare -p shows the attribute without a value, and printenv finds nothing. As soon as you write NEWV=hello, the variable enters the environment because the export attribute was already waiting.
Listing Exported Variables
export -p prints every exported variable in a form that you can paste back into a script. Filter it with grep to look at a few names:
export -p | grep -E ' (V1|V2|NUM)='
declare -x NUM="2"
declare -x V1="plain"
declare -x V2="assigned_and_exported"
Plain env shows the same variables as NAME=value lines:
env | grep -E '^(V1|V2|NUM)=' | sort
NUM=2
V1=plain
V2=assigned_and_exported
Exporting Does Not Affect the Parent
Exporting sends a value down to child processes only. A script that exports a variable cannot change the shell that started it, because the script runs as a child. If you need a script to change variables in your current shell, run it with source, as covered in the lesson on running scripts in different ways.
Removing the Export Attribute
The -n option removes the attribute but keeps the variable and its value in the current shell:
export SHARED="visible"
export -n SHARED
bash -c 'echo "child SHARED=[${SHARED-unset}]"'
echo "shell SHARED=[$SHARED]"
child SHARED=[unset]
shell SHARED=[visible]
To remove a variable completely, use unset, which a later lesson covers.
Exporting Functions
Functions can be exported too, with export -f. A child Bash then knows the function by name:
add() { echo $(( $1 + $2 )); }
export -f add
bash -c 'add 2 3'
5
The function body travels through the environment, so the child Bash could call add as if it had defined it. This works only between Bash shells. Other languages and other shells do not understand exported Bash functions. Exported functions are also a surprising entry point for security problems, a topic that comes back in the security lessons, so use them sparingly.
Arrays Cannot Be Exported
The environment can hold only text, and Bash has no way to pass an array through it. Bash accepts the command, but the child sees nothing:
arr=(a b c)
export arr
declare -p arr
bash -c 'echo "child arr=[${arr-unset}] length=${#arr[@]}"'
declare -ax arr=([0]="a" [1]="b" [2]="c")
child arr=[unset] length=0
The attribute -a (array) and -x (export) both appear in the parent, but the child did not receive the array. To pass a list, build one string, for example with spaces or colons between the items, and split it in the child. The PATH variable uses exactly this approach, with colons.
Invalid Names
export checks the name before it does anything else:
export 1abc=5
export: `1abc=5': not a valid identifier
The message appears for any name that breaks the naming rules from an earlier lesson, such as one that starts with a digit or contains a hyphen.
A Practical Example
A script often passes a setting to another script by exporting it. The first script below reads a mode and starts a second script, and the second script reads the same setting from its environment:
cat > report.sh <<'EOF'
#!/usr/bin/env bash
echo "report: LOG_LEVEL=${LOG_LEVEL:-info}"
EOF
chmod +x report.sh
LOG_LEVEL=debug ./report.sh
./report.sh
report: LOG_LEVEL=debug
report: LOG_LEVEL=info
The first call supplied LOG_LEVEL for one command only, using a prefix assignment. The second call found no value and used the default info. If the variable had been exported earlier with export LOG_LEVEL=debug, every later call would have seen debug until the shell closed.
How Long an Export Lasts
An export lasts as long as the shell in which you ran it. When you close the terminal, the variable disappears. To make a variable available in every new terminal, put the export line into one of Bash's startup files, which a later lesson covers.