export

Set export attributes for shell variables or functions.

Summary

export [-fn] [name[=word]]...
export -p

The main purpose

Options

-f: Point to function.
-n: Delete the exported attributes of the variable.
-p: Display all variables with exported attributes.
-pf: Display all functions with exported attributes.
-nf: Remove the exported attributes of the function.
--: The options after it are invalid.

Parameters

name (optional): variable name or defined function name.

value (optional): The value of the variable.

return value

export returns true unless you provide an illegal option or illegal name.

example

# Display all variables with exported properties.
# export -p
# export
# Display all functions with exported properties.
# export -pf
# First delete the variable name to be demonstrated
#unset a b
# Define variables and add export attributes at the same time
export a b=3
# Of course, you can also define it first and then add export attributes.
b=3
export b

# Modify the value of a variable with exported properties
export a=5 b=7
#Of course, you can also assign and modify it directly
a=5;b=7

# Delete the exported attribute of the variable
export -n a b
# First delete the function name to be demonstrated
unset func_1 func_2
#Create function
function func_1(){ echo '123'; }
function func_2(){ echo '890'; }

# Add export attributes to defined functions
export -f func_1 func_2

# Delete the exported attributes of the function
export -fn a b
# Add environment variables (JAVA) to `~/.bashrc`
PATH=/usr/local/jdk1.7.0/bin:$PATH
#Add the current location to the dynamic library environment variable
export LD_LIBRARY_PATH=$(pwd):${LD_LIBRARY_PATH}

Incorrect usage

Q&A

Q: What is the use of setting export attributes on variables or functions?

A: They will become environment variables and can be accessed in the script, especially if needed by the subprocess called in the script. (Reference link 4)

Q: If the script I write modifies the value of an existing environment variable, will it take effect in the current terminal when executed? Will it affect terminals opened before and after?

A: Only scripts called through the source method will take effect. You can view the source command for more information; other methods are just executed in a subshell. The previous ones will not be affected, unless the scripts loaded when starting the terminal such as ~/.bashrc are modified. (Reference link 1)

Q: My script file calls the functions and variables defined in ~/.bashrc. Why is the script called via sh in a newly opened terminal or run directly?

This script that the current user has execution permissions cannot use these functions and variables? A: Please add export statements to the ~/.bashrc file. See also the Knowledge Points paragraph.

Q: Can export properties also be set for arrays and associative arrays?

A: Yes (if your bash supports them), but there are some problems (Reference link 2).

Q: Why does it start with declare when I view the exported properties of a variable or function?

A: Because declare can also set the exported attributes of variables or functions, see the declare command for details.

Notice

Knowledge points

In info bash or bash online documentation Section 3.7.3 mentions the shell execution environment, which involves variables and functions as follows

So what does the parameter in the first sentence have to do with the variable? Mentioned in the first paragraph of Section 3.4:

A variable is a parameter denoted by a name.

Variables are named parameters.

Then the subshell does inherit variables or functions with exported attributes in the parent shell.

Reference link: Differences in script execution methods

Reference link

Further reading

Generally speaking, when configuring a cross-compilation tool chain, you need to specify the path of the compilation tool. At this time, you need to set environment variables. View existing environment variables:

[root@localhost ~]# export
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x hostname="localhost"
declare -x INPUTRC="/etc/inputrc"
declare -x LANG="zh_CN.UTF-8"
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x logname="root"
declare -x LS_COLORS="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40; 33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com= 01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01; 31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31: *.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*. jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif= 01;35:"
declare -x mail="/var/spool/mail/root"
declare -x OLDPWD
declare -x PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin: /root/bin"
declare -x pwd="/root"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_CLIENT="192.168.2.111 2705 22"
declare -x SSH_CONNECTION="192.168.2.111 2705 192.168.2.2 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="linux"
declare -x USER="root"