Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Bash export command

Reference for the Bash export builtin: pass variables to child processes, list the environment, remove exports with -n, and export functions with -f.

export marks a shell variable so that every program started from the shell receives it in its environment. A plain assignment NAME=value creates a variable that only the shell itself sees; export NAME (or export NAME=value) hands a copy to children such as scripts, ls or python.

The copy only travels downward: a child cannot change its parent's variables, and changing an exported variable later in the shell does not affect programs that are already running.

Terminal
export [-fn] [NAME[=VALUE] ...]
export -p

Try it

Bash Exported versus plain variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Listing, removing and exporting functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
export [-fn] [name[=value] ...]
export -p
Name Description
name Mark an existing (or new, empty) variable for export.
name=value Assign and export in one step. The value is expanded once, at that moment.
-n Remove the export attribute from the names; the variable stays set in the current shell.
-f Treat the names as functions instead of variables (Bash only).
-p Print all exported variables as declare -x NAME="value" lines.

Returns: 0, or non-zero if an option is invalid or a name is not a valid identifier (for example export 1abc=x).

Errors and edge cases: export of a read-only variable with a new value fails. Exporting does not copy shell arrays or associative arrays into the environment in any usable form.

Usage notes

  • Assignment vs export. FOO=bar is local to the shell; export FOO=bar is inherited. A prefix assignment FOO=bar cmd exports FOO for that one command only, which is the cleanest way to pass a setting for a single run.
  • Export once. After export FOO, later plain assignments (FOO=new) update the exported value automatically. No need to repeat export.
  • Spaces and quotes. Always quote values with spaces: export MSG="two words". export MSG=two words exports MSG=two and marks words for export too.
  • Environment size. The environment plus arguments is limited (getconf ARG_MAX, typically 2 MB on Linux). Huge exported variables can make every command fail with "Argument list too long".
  • Secrets. Exported variables are visible to every child process and, for the same user, in /proc/PID/environ. Do not export tokens that only one command needs; prefix the command instead.
  • Profiles. Put exports for login shells in ~/.profile or ~/.bash_profile, and interactive-only settings in ~/.bashrc. Changes apply to new shells; use source to reload the current one.
  • Compare tools. env runs a command with a modified environment, printenv NAME prints one value, declare -x is another spelling of export (see declare), and unset deletes the variable altogether (unset).
  • POSIX shells support export name=value and export -p; -n and -f are Bash extensions.