Five bash shell execution modes fork, source, exec, (), {}

Source: Internet
Author: User
Tags call shell

How shell scripts are executed:

NOTE: WD stands for "Script saved directory"


1.fork
Syntax:/wd/shell.sh
Fork is the most common, is directly in the script with/wd/shell.sh to call shell.sh this script. Run a child shell executes the calling script when the child shell executes, and the parent shell is still there. When the child shell finishes executing, it returns to the parent shell. The child shell inherits environment variables from the parent shell. But environment variables in child shells are not brought back to the parent shell

2.exec
Syntax: exec/wd/shell.sh
Unlike fork, Exec does not need to open a new Sub-shell to execute the called script. The invoked script executes within the same shell as the parent script. However, after calling a new script with exec, the content after the Exec line in the parent script is no longer executed. This is the difference between exec and source.

3.source
Syntax: source/wd/shell.sh
The difference from fork is that it does not open a child shell to execute the called script, but executes it in the same shell. So the variables and environment variables declared in the invoked script can be obtained and used in the main script.

4. ()
Syntax: (statement; statement; statement; ...)
The commands in the parentheses command list will be run in a child shell.

Like what:
$ (CD.; LS-L)
Cd.. Command changes the PWD of the child shell without affecting the interactive shell.
However the order
$ CD.; Ls-l
Then there are different effects on the CD. command is executed directly under the interactive shell, changing the pwd of the interactive shell,
This is the equivalent of executing the shell script as follows:
$ source./script.sh
Or
$ . ./script.sh

The source or. command is the shell's built-in command, which does not create a child shell, but rather executes the commands in the script line-by-row under the interactive shell.


SOURCE command,. command
Grammar:. filename [arguments]
source filename [arguments]
Role:
When called at the command line, this command executes a script. When called in the script, the source file-name will load the File-name file, or. (Point command) a file will introduce code into the script and attach the code to the script (the same effect as the # include directive in C). The end result is like inserting the contents of the corresponding file on a line that uses "source".
This command is useful when multiple scripts need to reference the same data, or if you need to use a library of functions.

5.{}
{statement, statement, ...} The child shell is not started.

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.