How to use the source Command in linux

Source: Internet
Author: User
This article describes in detail how to run the LinuxSource command and script, which has some reference value. if you are interested, refer to the Linux Source Command and script execution method parsing.

When I modify the/etc/profile file, I want it to take effect immediately without logging on again. at this time, I want to use the source command, such as source/etc/profile.
The source is learned and compared with the sh execution script.

Source command:

The source Command is also called the "Dot Command", that is, a dot symbol (.), which is an internal bash command.

Function: enables Shell to read the specified Shell program file and execute all statements in the file in sequence.
The source command is usually used to re-execute the modified initialization file to make it take effect immediately without logging out and logging on again.

Usage:

Source filename or. filename
The source Command (from the C Shell) is the built-in command of the bash shell; the dot command (.) is a dot symbol (from the Bourne Shell) is another name of the source.

What is the difference between source filename and sh filename and./filename script execution?

1. when the shell script has the executable permission, there is no difference between using sh filename and./filename to execute the script .. /Filename indicates that the current directory is not in the PATH, and all "." indicates the current directory.
2. sh filename re-creates a sub-shell and executes the statements in the script in the sub-shell. the sub-shell inherits the environment variables of the parent shell, the new and changed variables created by the sub-shell are not brought back to the parent shell unless export is used.
3. source filename: The Command simply reads the statements in the script and runs them in the current shell. no new sub-shell is created. All statements for creating and changing variables in the script are saved in the current shell.

Example:

1. create A test. sh script with the following content: A = 1

2. execute chmod + x test. sh.

3. after running sh test. sh, echo $ A is blank because A = 1 is not returned to the current shell.

4. after running./test. sh, the same effect will be achieved.

5. run source test. sh or. test. sh, and then echo $ A. 1 is displayed, indicating that the variable A = 1 is in the current shell.

The above is all the content of this article. I hope it will help you learn and support PHP.

For more details about how to use the source Command in linux, refer to the PHP Chinese network!

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.