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.
export [-fn] [NAME[=VALUE] ...] export -p
Try it
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.
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
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=baris local to the shell;export FOO=baris inherited. A prefix assignmentFOO=bar cmdexportsFOOfor 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 repeatexport. - Spaces and quotes. Always quote values with spaces:
export MSG="two words".export MSG=two wordsexportsMSG=twoand markswordsfor 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
~/.profileor~/.bash_profile, and interactive-only settings in~/.bashrc. Changes apply to new shells; usesourceto reload the current one. - Compare tools.
envruns a command with a modified environment,printenv NAMEprints one value,declare -xis another spelling ofexport(seedeclare), andunsetdeletes the variable altogether (unset). - POSIX shells support
export name=valueandexport -p;-nand-fare Bash extensions.
Related
unset,declare,localandreadonly,source,set- Lessons: Environment, Variables