Bash Export command

A .shell and export

After "1" user logs on to the Linux system, the system will launch a user shell. In this shell, you can use shell commands or to declare variables, you can create and run a shell script. When you run the shell script, the system creates a sub-shell. At this point, the system will have two shell, is a login shell system startup, the other is a system created to run shell scripts. When a script has finished running, the shell will terminate script, return to the shell execution before the script. In this sense, the user can have many shell, each shell is made of a shell (called the parent shell) derived. Variable defined in the sub-shell is valid only in the sub-shell. If you define a variable in a shell script, when the script runs, the definition of variables just a local variable in the script, the other shell can not refer to it, to make the value of a variable can be found in other the shell is changed, you can use the export command output variables defined. export command will make the system when you create each new shell, the definition of a copy of this variable. This process is called variable output.

Two .export Function: Set or display environment variables.

1. Syntax: export [-fnp] [Variable Name] = [Variable Settings]
2. Additional information: a program executing in the shell, shell provides a set of environment variables. export can add, modify or delete environment variables, program for subsequent executions of use. effectiveness of export is limited to the sub-landing operation.
3. Parameters: -f Representative [Variable Name] as the function name. -n delete the specified variable. Variable is not actually deleted, but not output to the subsequent instruction execution environment.
 -p lists all the shell environment variables given program.

III. Learning export understand shell scripting period

1. Add the export environment variables sh file, after the source file .sh, will identify the environmental variables
2. After if not export, can also identify the source environment variables

For more performance can be concluded that:
 1. 执行脚本时是在一个子shell环境运行的,脚本执行完后该子shell自动退出;
 2. 一个shell中的系统环境变量会被复制到子shell中(用export定义的变量);
 3.  一个shell中的系统环境变量只对该shell或者它的子shell有效,该shell结束时变量消失并不能返回到父shell中)
 4. 不用export定义的变量只对该shell有效,对子shell也是无效的。

Four last question:

Why is there an environment variable and executed directly after the source will not perform the same?

Direct execution of a script file is run in a sub-shell, and the source is running in the current shell environment

Published 25 original articles · won praise 0 · Views 431

Guess you like

Origin blog.csdn.net/m0_38028438/article/details/104609844