About the Shell's source, point (.), and Export

Source: Internet
Author: User

Organize multiple sources from the network.

1, source and Point command, the same function, the source is different.

The source command is a built-in command of the Bash shell, which comes from C shell. Another notation for the source command is the point symbol, which is used in the same way as source, from the Bourne shell.

2.Shell Script Execution method

There are two ways to execute shell scripts, one is to create a new shell, and then execute the corresponding shell scripts, one that executes under the current shell and no longer enables other shells.

The new way to create a shell and then execute scripts is to add a statement at the beginning of the scripts file: #!/bin/sh. The usual script file (. sh) is this usage. This method first enables the new Sub-shell (the new child process) and then executes the command under it.

Another way to do this is by the source command, which no longer produces a new shell, but executes all commands under the current shell. Enter man source under Bash, find the source command interpreter, and you can see the explanation "Read and execute commands from filename in the current shell environment and ..." 。 As you can tell, the source command executes each command in the parameter file in the current process, not the other driver process (or Sub-shell).

3, about the source command.

(1), the source command can forcibly let a script to immediately affect the current environment.

(2), the source command enforces all commands in the script, ignoring the permissions of the file.

(3), the source command is typically used to re-execute a newly modified initialization file, such as. Bash_profile and. Profile, and so on.

4, about the export order.

Export and source are actually two completely different commands. Export general explanation is "set or show environment variables". This is functionally correct, but why use the word "export"?

This is because the variables defined in the child shell (see 2nd) are valid only within the child shell. If a variable is defined in a shell script, when the script is run, the defined variable is only a local variable within the script, and the other shell cannot reference it, so that the value of one variable can be changed in the other shell. You can use the Export command to output a defined variable. The export command will allow the system to define a copy of this variable when creating each new shell. This process is called variable output.

5. Source filename differs from SH filename and./filename execution script.

(1), when the shell script has executable permissions, it is no different to execute the script with the./filename with SH filename:/filename because the current directory is not in path, all "." is used to represent the current directory.

(2), SH filename re-establishes a child shell, executes the statement inside the script in the child shell, the child shell inherits the environment variables of the parent shell, but the new and changed variables of the child shell will not be brought back to the parent shell unless export is used.

(3), source filename: This command is simply to read the script inside the sentence in order to execute in the current shell, not to create a new child shell. Then all the new and changed statements in the script will be stored in the current shell.


Walker * * *


This article is from "Walker's Journal" blog, please be sure to keep this source http://walkerqt.blog.51cto.com/1310630/1690202

About the Shell's source, point (.), and Export

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.