[Linux Command Explanation Encyclopedia] 065. In-depth understanding of the export command: setting export attributes and environment variable management

export

Set the export attribute for a shell variable or function.

overview

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

The main purpose

  • Define one or more variables and set export properties.
  • Modify the value of one or more variables and set export properties.
  • Removes the exported properties of one or more variables.
  • Displays all variables that have exported properties.
  • Adds an export attribute for one or more defined functions.
  • Removes the export attribute of one or more functions.
  • Shows all functions that have an exported attribute.

options

  • -f: points to a function.
  • -n: Removes the export attribute of the variable.
  • -p: Display all variables with export properties.
  • -pf: Display all functions with export attributes.
  • -nf: Removes the export attribute of the function.
  • --: options after it are invalid.

parameter

  • name(Optional): Variable name or defined function name.
  • value(Optional): The value of the variable.

return value

exportReturns true unless you provide an invalid option or invalid name.

example

# 显示全部拥有导出属性的变量。
# export -p
# export
# 显示全部拥有导出属性的函数。
# export -pf

# 首先删除要演示的变量名
# unset a b

# 定义变量的同时增加导出属性
export a b=3

# 当然也可以先定义后增加导出属性
b=3
export b

# 修改拥有导出属性的变量的值
export a=5 b=7

# 当然也可以直接赋值修改
a=5;b=7

# 删除变量的导出属性
export -n a b

# 首先删除要演示的函数名
unset func_1 func_2

# 创建函数
function func_1(){
    
     echo '123'; }
function func_2(){
    
     echo '890'; }

# 为已定义函数增加导出属性
export -f func_1 func_2

# 删除函数的导出属性
export -fn a b

# 添加环境变量(JAVA)到`~/.bashrc`
PATH=/usr/local/jdk1.7.0/bin:$PATH

# 添加当前位置到动态库环境变量
export LD_LIBRARY_PATH=$(pwd):${LD_LIBRARY_PATH}

wrong usage

  • Add export attribute to undefined functions.
  • Execute the remove export attribute operation on functions/variables that do not have an export attribute.
  • --Use options after .

Q&A

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

    • A: They will become environment variables, which can be accessed in the script, especially if the subprocess called in the script needs it. (reference link 4)
  • Q: If the script I wrote 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 sourcescripts invoked via the method will take effect, you can check sourcethe command for more information; other methods are only executed in a subshell. The previous ones will not be affected, and the later ones will not be affected unless ~/.bashrcthe script loaded when starting the terminal is modified. (reference link 1)
  • Q: I call ~/.bashrcthe functions and variables defined in the script file. Why can't these functions and variables be used when the script is invoked through sh in the newly opened terminal or the script that the current user has execution permission is run directly?

    • A: Please ~/.bashrcadd exporttheir statements in the file. See also the Knowledge Points paragraph.
  • Q: Can arrays and associative arrays also set export properties?

    • A: Yes (if your bash supports them), but there are some problems (see link 2).
  • Q: Why do I display declare at the beginning when I view the export properties of variables or functions?

    • A: Because declarecan also set the export properties of variables or functions, see declarethe command for details.

Notice

This command is basha built-in command, please refer to command for related help information help.

knowledge points

The shell execution environment is mentioned in section 3.7.3 of info bash or bash online documentation , and the contents involving variables and functions are as follows:

  • shell parameters that are set by variable assignment or with set or inherited from the shell’s parent in the environment
  • shell functions defined during execution or inherited from the shell’s parent in the environment

So what does the parameter in the first sentence have to do with the variable? In the first paragraph of Section 3.4 it is mentioned:

A variable is a parameter denoted by a name.

Variables are named parameters.

Then the subshell does inherit the variable or function with the export attribute from the parent shell.

You can refer to the link: Differences in the way of executing scripts

reference link

  1. Discussion about bashrc profile file
  2. Discussion about export array
  3. export -pf Usage
  4. The difference between environment variables and shell variables

further reading

Generally speaking, when configuring the cross-compilation toolchain, you need to specify the path of the compilation tool, and you need to set the environment variable at this time. 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"

Learn from scratchpython

[Learn python from zero] 92. Use Python's requests library to send HTTP requests and process responses
[Learn python from zero] 91. Use decorators and dictionaries to manage simple web applications for request paths
[Learn python from zero] 93. Use dictionary management Request path
[Learn python from zero] 89. Use WSGI to build a simple and efficient web server
[Learn python from zero] 88. Detailed explanation of WSGI interface: realize simple and efficient web development
[Learn python from zero] 87. Manually build HTTP server Python Implementation and multi-threaded concurrent processing
[learning python from zero] 86. In-depth understanding of HTTP protocol and its role in browser and server communication
[learning python from zero] 85. Parallel computing technology application of Python process pool
[learning python from zero ] 84. In-depth understanding of threads and processes
[learning python from zero] 83. Python multi-process programming and the use of process pools
[learning python from zero] 82. Realization of chat programs based on multi-threading
[learning python from zero] 81. Python and more Application of thread communication and queue
[learning python from zero] 80. Thread access to global variables and thread safety issues
[learning python from zero] 79. Thread access to global variables and thread security issues
[learning python from zero] 78. File download cases
[ Learning python from zero] 77. TCP server programming and precautions
[learning python from zero] 76. Server and client: key components of network communication
[learning python from zero] 75. TCP protocol: reliable connection-oriented transmission layer communication protocol
[Learn python from zero] 74. UDP network program: detailed explanation of port problems and binding information
[Learn python from zero] 73. UDP network program - send data
[Learn python from zero] 72. In-depth understanding of Socket communication and socket creation The method of
[learning python from zero] 71. Network ports and their functions
[learning python from zero] 70. Network communication methods and their applications: from direct communication to routers to connect multiple networks
[learning python from zero] 69. Network communication and IP address classification analysis
[learning python from zero] 68. Greedy and non-greedy modes in Python regular expressions
[learning python from zero] 67. re module in Python: regular replacement and advanced matching technology
[learning python from zero] 66 .In-depth understanding of regular expressions: a powerful tool for pattern matching and text processing
[Learn python from zero] 65. Detailed explanation of Python regular expression modifiers and their applications
[Learn python from zero] 64. The re.compile method in Python regular expressions Detailed explanation
[learning python from zero] 63. Introduction to the re.Match class in regular expressions and its attributes and methods
[learning python from zero] 62. Python regular expressions: a powerful string matching tool
[learning python from zero] 61. Detailed explanation and application examples of property attributes in Python
[learning python from zero] 60. Exploration generator: a flexible tool for iteration
[learning python from zero] 59. Iterator: an efficient tool for optimizing data traversal
[learning python from zero] 58. Custom exceptions in Python and methods of raising exceptions
[Learn python from zero] 57. Use the with keyword in Python to correctly close resources
[Learn python from zero] 56. The importance and application of exception handling in programming
[Learn python from zero] 55. Serialization and processing in Python Deserialization, application of JSON and pickle modules
[learning python from zero] 54. Writing data in memory
[learning python from zero] 53. CSV files and Python CSV modules
[learning python from zero] 52. Reading and writing files - Python file operation guide
[learning python from zero] 51. Opening and closing of files and its application in Python
[learning python from zero] 49. Object-related built-in functions in Python and their usage
[learning python from zero] 48 .Detailed explanation of inheritance and multiple inheritance in Python
[learning python from zero] 47. The concept and basic use of inheritance in object-oriented programming
[learning python from zero] 46. __new__ and __init__ method analysis and singleton in Python Design patterns
[learning python from zero] 45. Class methods and static methods in Python
[learning python from zero] 44. Private attributes and methods in object-oriented programming
[learning python from zero] 43. Examples in Python object-oriented programming Attributes and class attributes
[Learn python from zero] 42. Built-in attributes and methods in Python
[Learn python from zero] 41. Python magic method (2)
[Learn python from zero] 40. Python magic method (1)
[Learn python from zero] 39. Object-oriented basic grammar and application examples
[Learn python from zero] 38. Use and import of Python packages
[Learn python from zero] 37. Use and precautions of Python custom modules
[From zero Learning python] 36. Methods and skills of using pip in Python for third-party package management
[learning python from zero] 35. Python common system modules and their usage
[learning python from zero] 34. Detailed explanation of the import and use of Python modules
[ Learn python from zero] 33. The role of decorators (2)
[Learn python from zero] 32. The role of decorators (1)
[Learn python from zero] 31. In-depth understanding of higher-order functions and closures in Python
[from Zero learning python] 30. In-depth understanding of recursive functions and anonymous functions
[learning python from zero] 29. "Detailed explanation of function parameters" - understand the different usages of Python function parameters
[learning python from zero] 28. Local variables and globals in Python Variables
[Learn python from zero] 27. Use and nested calls of Python functions
[Learn python from zero] 25. Functions: a tool to improve code writing efficiency
[Learn python from zero] 24. String operations and traversal methods in Python
[Learn python from zero] 23. How to use and common operations of sets (set) in Python
[Learn python from zero] 22. Addition, deletion, modification and query of dictionaries in Python and variables of dictionaries
[Learn python from zero] 21. In Python tuples and dictionaries for
[Learn python from zero] 20. Python list operation skills and examples
[Learn python from zero] 19. Application of looping through lists and list nesting
[Learn python from zero] 18. Detailed explanation of basic operations of Python lists (1)
[From Zero learning python] 17. Format method of Python string (2)
[Learn python from zero] 16. Format method of Python string (1)
[Learn python from zero] 15. In-depth understanding of string and character set encoding
[from Zero learning python] 14. Common operations of Python strings (2)
[Learn python from zero] 13. Common operations of Python strings (1)
[Learn python from zero] 12. Python string operations and applications
[Learn python from zero] 11.Python loop statement and control flow
[learn python from zero] 10. Detailed explanation of Python conditional statement and if nesting
[learn python from zero] 09. Conditional judgment statement in Python
[learn python from zero] 08. Python understands bit operations Operators, operator priority
[learn python from zero] 07. Detailed explanation of Python operators: assignment, comparison and logical operators
[learn python from zero] 06. Use arithmetic operators in Python for calculation and string concatenation
[learn from zero python] 05. Output and input in Python
[learn python from zero] 04. Basics of Python programming: variables, data types and identifiers
[learn python from zero] 03. Python interactive programming and detailed annotations
[Learn python from zero] 02. Introduction to development tools
[Learn python from zero] 01. Install and configure python

Guess you like

Origin blog.csdn.net/qq_33681891/article/details/132685151