The export command is used to output shell variables as environment variables or shell functions as environment variables.

When a variable is created, it is not automatically known to the shell process created after it. The export command can pass the value of a variable to a subsequent shell.

Command syntax

Export [parameter]

The command parameter

  • -f: indicates the function.
  • -n: Deletes the exported attributes of a variable.
  • -p: displays all variables that have exported attributes.
  • -pf: displays all functions that have exported attributes.
  • -nf: deletes the exported attributes of a function.

Lists all current environment variables

> export - p declare - x CLASSPATH = ". : / opt/Java/jdk1.8.0 _231 / lib: / opt/Java/jdk1.8.0 _231 / jre/lib "declare - x HISTCONTROL="ignoredups" declare -x HISTSIZE="1000" declare -x HOME="/root" declare -x HOSTNAME="JD" declare -x JAVA_HOME="/opt/ Java /jdk1.8.0_231" DECLARE -x JAVA_OPTS=" -server-xMS4096m -XMX4096m -xx :PermSize=256M -xx :MaxNewSize=512m -xx :MaxPermSize=512m" DECLARE -x JRE_HOME="/opt/ Java /jdk1.8.0_231/jre" DECLARE -x LANG=" en_US.utF-8"  declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s" declare -x LOGNAME="root"Copy the code

Defining environment variables

> export RUMENZ
Copy the code

Define environment variable assignments

> export RUMENZ=7
Copy the code

Export a function

Export a function func_1

> function func_1(){ echo "123"; }
Copy the code

Test function output

> func_1
123
Copy the code

Call func_1 in a bash script

> vim test.sh #! Sh test.sh: line 3: func_1: Command Not foundCopy the code

The func_1 function can also be called normally in a script, where export is used

> export-f func_1 // View the defined function > export-pf func_1 () {echo '123'} declare -fx func_1Copy the code

Delete the export attribute of func_1

> export-fn func_1 // Run the script > bash test.sh test.sh: line 3: func_1: command Not foundCopy the code

Make exported variables permanent

In the command line to export a variety of variables, the function is only temporary, the next login, previously defined variables, the function will not exist. If we want to define a variable function that can be used every time we log in to the system, then we need to define the definition and export of the variable in a file, and then execute the file every time we log in to the system.

Normal Login Configuration files are loaded in sequence. Normal login refers to the login status when a user enters the user name and password.

If you want to apply to all users of the system, you can define variables and exports in the following file

/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
Copy the code

For example, define the JAVA_HOME environment variable

Export JAVA_HOME=/usr/local/jdk1.8 export PATH=${JAVA_HOME}/bin:$PATH // /etc/profileCopy the code

If you want to apply to only one user, you can define variables and export them in the following files in the user’s home directory

~/.bash_profile
~/.bashrc
Copy the code

Customize the front view of the command line. Display the user name, host name, and shell name

> vim ~/.bashrc # export PS1="\u@\h>\s "// immediately > source ~/.bashrcCopy the code

The command line is changed from [root@local ~]# to root@local>-bash

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station