Scope of environment variables in Shell

Source: Internet
Author: User

Shell and export commands
After a user logs on to Linux, the system starts a User Shell. In this shell, you can use the shell command

Or declare variables, you can also create and runShell script program. When you run the shell script program, the system creates a sub-shell.

At this point, there will be two shells in the system, one is the Shell started by the system at login, and the other is the shell created by the system for the script program running.

. After a script is run, the script shell is terminated and returns the shell before the script is executed.

 

In this sense, you can have many shells, each of which is derived from a shell (called the parent shell.

The variables defined in the sub-shell are valid only in the sub-shell. If a variable is defined in a shell script program,

When the script is running, the defined variable is only a local variable in the script program, and other shells cannot reference it,

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:

 

Myfile = "list"
Export myfile
Echo "displaying $ myfile"
Pr-T-N $ myfile
Printfile

 
Printfile script program list:

Echo "printing $ myfile"
LPR $ myfile &

 

Running result:

$ Dispfile
Displaying list
1 screen
2 Modem
3 Paper
Printing list
$

 

ExportFunction 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 limited to 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.

 

Shell environment and variable survival learned from the Export command

Recently, when I was learning the bash Export command, I encountered a question: (in the book, export is used to change the custom variable

System environment variables): I defined a variable in a script file, and then the export variable, according to my own ideas,

After the script is executed, you can use echo to display its value at the prompt, but the result is not like this. 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

I can use the source + script file. I tried it, but a new problem came out. In the script

After the Export command is deleted, use the same source command. It seems that this export is useless.

After several attempts, I found something that I guessed. If anything is wrong, please correct me. Thank you.

When executing a script, a sub-shell environment will be started first (I don't know if this is the case for executing other programs), and then the parent

All system environment variables in shell are copied, and the statements in this script are executed in the sub-shell. (That is, the parent Shell

Environment variables can be called in the sub-shell, but in turn it will not work. If the environment variables are defined in the sub-shell, only the 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 will open another sub-shell in the last line of the script. This shell should be the sub-shell of the shell where the script file is located. This script

After the execution, 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, the variable will not be seen in the sub-shell,

Because it is not a system environment variable, for example, the script content is:
Test = 'value'
 
After executing the command with source, you can see this variable in shell, but when executing Bash to open a sub-shell, test won't

Copied to the sub-shell, because the execution script file is actually running in a sub-shell, so I will create another script

When the file is executed, nothing is input, such as Echo $ test. So pay special attention to this, obviously at the prompt

Echo $ test can be used to output the variable value. Why can't I put it into the script file?

So the conclusion is:

1. The script is run in a subshell environment. After the script is executed, the subshell automatically exits;

2. system environment variables in a shell will be copied to the sub-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.

 

I sorted out the post: Why is one script executed directly and another line executed with source? 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.

Why are they different? Directly execute a script file and run it in a sub-shell, while source is in

Run in the Current Shell environment. Based on the previous content, you have understood the truth.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.