Linux source and export commands

Source: Internet
Author: User
Source command usage:
Source filename
Purpose: read and execute the command in filename in the current bash environment.
Note:This command is usually replaced by the "." command.

For example, Source
. Bash_rc is equivalent to... bash_rc.

The source command (from the C shell) is Bash
Shell built-in commands. The dot command is a dot symbol (From Bourne
Is another name of source. Similarly, the variables configured in the current script will also be used as the script environment. The source (or point) command is usually used to re-execute the modified initialization document, as shown in
. Bash_profile and. profile. For example
If the term variable is modified, you can use the source command to re-Execute. bash_profile
You do not need to log out and log on again.

For example, in a script, export $ KKK = 111
If you use./A. Sh to execute the script, after the script is executed, you run echo $ KKK and no value is found. If you use source to execute the script, then Echo
Kkk = 111. Because the./A. Sh command is called to run the shell in a sub-shell, the structure is not reflected in the parent shell after execution,
Different from source, it is executed in this shell, so we can see the result.

Small Test

1. Create test. Sh
#! /Bin/bash
Export S =/home/JBoss/
2
Run the following command: source test. Sh.

Echo $ s
Result output:/home/JBoss/

3
New Shell
Run the following command:
./Test. Sh

Echo $ s
Result:
No output S Value

Conclusion:
1. The script is run in a sub-shell environment. After the script is executed, the sub-shell 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 only valid for the shell or its sub-shell. When the shell ends, the variables disappear (and cannot be returned to the parent shell ).

4. Variables not defined by export are only valid for this shell and are also invalid for the sub-shell.

5. directly run a script file in a sub-shell, while source runs in the Current Shell environment.
source allows scripts to affect their parent shell environment, which is the opposite of export affecting the subshell environment.

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.