[Reprinted] EXEC command in Shell

Source: Internet
Author: User

Original article address:
Http://dev.firnow.com/course/6_system/linux/Linuxjs/20071027/80454.html


Both exec and source are Bash Internal commands (builtins commands). Enter man exec or man source in Bash to view all internal command information.

Bash shell commands are divided into two types: External commands and Internal commands. External commands are called by the system or are independentProgramImplemented, such as SED and awk. Internal commands are implemented by special file formats (. Def), such as CD, history,ExecAnd so on.

Before explaining the differences between exec and source, we should first describe the concept of fork.

Fork is a Linux System Call used to create child processes ). A child process is a copy of the parent process. It obtains a certain amount of resource allocation from the parent process and inherits the environment of the parent process. The unique difference between a child process and a parent process lies in the PID (process ID ).

Environment variables (variables passed to sub-processes, genetic is the fundamental difference between local variables and environment variables) can only be transmitted from the parent process to the sub-process in one way. How the environment variables of a non-process change will not affect the environment variables of the parent process.

Shell script:

There are two ways to execute shell scripts. One is to generate a new shell and then execute the corresponding shell scripts. The other is to execute the shell in the current shell and no longer enable other shells.
To generate a new shell and then execute scripts, add the following statement at the beginning of the scripts file:

#! /Bin/sh

This is the general usage of script files (. Sh. In this way, the new sub-shell (new sub-process) is started, and then the command is executed under it.
Another method is the source command mentioned above. instead of creating a new shell, execute all the commands in the current shell.

Source:

The source command is the dot (.) command.

Enter man source in bash and find the explanation of the source command. You can see the explanation "read and execute commands from filename in the Current Shell environment and ...". It can be seen that the source command is to execute the commands in the parameter file in the current process, rather than starting another sub-process (or sub-shell ). Source filename or. filename: Execute the command in filename.

Exec:

Enter man exec in bash, find the exec command explanation, and you can see that there is an explanation like "no new process is created.", which means that the exec command does not generate a new sub-process. So what is the difference between exec and source?

When the exec command is executed, the current shell process is closed, and the subsequent command is switched to continue execution.

references
[1] http://www.study-area.org/cyril/scripts/scripts/node1.html
[2] http://www.freeos.com/guides/lsst/

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.