The POSIX shell built-in export marks the specified shell variable for export. This means that the name of this variable and its value will subsequently be known and accessible to all commands.
Exported variables are deleted when the shell in which they were defined and exported is terminated. Frequently used variables should therefore be defined and exported in the $HOME/.profile file.
Positional parameters and shell functions cannot be exported. Some standard variables of the shell are available in all subshells without needing to be exported. These include HOME, IFS, PATH, PS1 and PS2. If you wish to assign some other value to these variables, and if the modified value is to be valid in every subshell, you will need to export these variables. Otherwise, the default value is valid in each subshell.
The built-in sh command set outputs all variables and associated values which are defined in the current shell. This therefore includes variables which you have not exported. The command export outputs the names and values of all shell variables which are passed to each called command and each subshell.
Syntax
Format 1: | export[ name[=value]]... |
Format 2: | export -p |
Name of the shell variable which you wish to export. You may also assign a value value to this variable after calling export. You may specify as many shell variables as you wish, each separaed by a space. name not specified:
If -p is set, export outputs the names and values of all the variables which have been exported. The output has the following format and is sent to the standard output:
The option -p provides transferrable access to the values which can be saved and then subsequently restored (e.g. by means of a dot procedure). The shell formats the output and ensures that the quotation marks are used correctly. This means that output is suitable for re-entry in the shell in the case of commands which have the same export results. |
Locale
The following environment variables affect the execution of export: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example
The shell script ps1 is to display the value of the PS1 variable. The script file contains the following: : Invocation with sh ps1 echo PS1: $PS1 The following interactive session demonstrates why the shell variable PS1 has to be exported:
When you define a shell variable, it is only known in the current shell. Since a shell script is always run by a subshell, variables for shell scripts have to be exported. |
See also
env, set |