Shell and export commands
After a user logs on to Linux, the system starts a User Shell. In this shell, you can use shell commands or declare variables, or create and run
Shell script program. When you run the shell script program, the system creates a sub-shell. At this time, the system will have two shells, one is the Shell started by the system at login, the other
Is the shell created by the system for running the script program. When a script program runs successfully, its script shell is terminated and can be returned to the shell before the script is executed. In this sense
A user can have many shells. Each shell is derived from a shell (called the parent shell. In
The variables defined in shell are valid only in the subshell. If a variable is defined in a shell script program, when the script is running, the defined variable is only in the script program.
Other shells cannot reference a local variable. To change the value of a variable in other shells, you can use the Export command to output the Defined variables.
The Export command enables the system to define a copy of this variable when creating a new shell. This process is called variable output. [Example] In this example, the variable myfile is defined in the dispfile script. Use the Export command to output the variable myfile to any sub-shell, for example, the sub-shell generated when the printfile script program is executed. List of dispfile script programs: /************* begin dispfile ****************/myfile = "list" Export myfileecho" displaying $ myfile "PR-T-N $ myfileprintfile/************** end dispfile ************ * **/printfile script: /************** begin printfile ***************/echo "printing $ myfile" LPR $ myfile &/************** end printfile ***************/$ dispfiledisplaying list1 screen2 modem3 paperprinting list
$
Linux Command: system settings -- Export
Function Description:
Set or display environment variables.
Syntax:
Export [-FNP] [variable name] = [variable setting value]
Note:
When executing a program in shell, shell provides a set of environment variables. Export allows you to add, modify, or delete environment variables for subsequent programs. The effect of export is only true for this login operation.
Parameters:
-F indicates the function name in [variable name.
-N: Delete the specified variable. The variable is not actually deleted, but is not output to the execution environment of subsequent commands.
-P: list all environment variables that the shell assigns to the program.
Http://linux.chinaitlab.com/command/723900.html
Shell environment and variable survival learned from the Export command
I am also a newbie myself. I haven't been in touch with Linux for a long time. I recently encountered a problem when I learned the bash Export command. (The book says that export is used to change the custom variable into a system loop.
Environment variable): I defined a variable in a script file, and then the export variable, according to my own ideas, after executing this script, at the prompt, you can use echo to display its value,
But this is not the case. After the script is executed, you cannot see this variable in set. Why? I was puzzled and finally posted the problem. A senior told me to use the source + script.
The file is ready. I tried it, but a new problem came out. After the Export command in the script is deleted, it can be used as source. There seems to be no such export.
Yes.
After several attempts, I found something that I guessed. If anything is wrong, please correct me. Thank you.
Execute a script
The sub-shell environment will be started first (do not know if other programs are executed like this), and then all the system environment variables in the parent shell will be copied, the statements in this script will be in the sub-shell
Shell. (That is, the environment variable of the parent shell can be called in the sub-shell, but in turn it won't work. If the environment variable is defined in the sub-shell
Or its sub-shell is valid. When the sub-shell ends, it can also be understood that the variable disappears after the script is executed .) To prove this, see the script content:
Test = 'value'
Export Test
After such a script is executed, test does not actually exist. Next, let's look at the following:
Test = 'value'
Export Test
Bash
Here we open another sub-shell in the last line of the script, which should be the sub-shell of the shell where the script file is located. After the script is executed, we can see the variable test, because it is in its sub-shell, when exit is used to exit the sub-shell, the test variable disappears.
If you use source to execute the script without export, you will not see this variable in the sub-shell, because it is not a system environment variable. For example, the script content is:
Test = 'value'
After running with source, you can see this variable in shell, but when executing Bash to open a sub-shell, test will not be copied to the sub-shell, because the execution script
This file is actually running in a sub-shell, so when I create another script file for execution, nothing will be input, such as Echo
$ Test. So pay special attention to this. We can use echo $ test to output the variable value at the prompt. Why can't we put it into the script file?
So
The conclusion is: 1. The script is run in a sub-shell environment, and the sub-shell automatically exits after the script is executed; 2. system environment variables in a shell will be copied to the child
In Shell (variables defined using export); 3. The system environment variables in a shell are valid only for the shell or its sub-shell. When the shell ends, the variables disappear.
(It cannot be returned to the parent shell ). 3. Variables not defined by export are only valid for this shell and are also invalid for the sub-shell.
Later, according to
Tip: I sorted out the post: Why is the execution of a script different from that of the source script? This is also a problem I encountered. The original manual is as follows: Read
And execute commands from filename in the Current Shell Environment and
Return the exit status of the Last Command executed from
Filename. Understand why is it different? Directly executing a script file runs in a sub-shell, while source runs in the Current Shell environment. According
You have already understood the above.
The problems that have plagued me for a few days can finally be solved successfully.