EXEC command-related __linux Linux

Source: Internet
Author: User
Tags stdin

EXEC command related in Linux:

Both exec and source are part of the Bash Internal command (builtins commands), where you enter man exec or man source to view all internal command information.
The bash shell commands fall into two categories: external commands and internal commands. External commands are implemented through system calls or stand-alone programs, such as SED, awk, and so on. Internal

Commands are implemented by a special file format (. def), such as CDs, history, exec, and so on.

Before explaining the difference between EXE and source, explain the concept of fork first.

Fork is a system call to Linux to create a subprocess (child process). A child process is a copy of the parent process, from the parent process

Obtain a certain resource allocation and the environment that inherits the parent process. The only difference between a child process and a parent process is the PID (process ID).

Environment variables (variables passed to the subprocess, which are fundamentally different from the local and environment variables) can only be passed to the child process from the parent process. Regardless of the child process's environment

Changes to a variable do not affect the environment variables of the parent process.

Shell script:

There are two ways to execute shell scripts, one is to create a new shell, and then execute the corresponding shell scripts, which is executed under the current shell, not

Then enable the other shell.

The way to create a new shell and then execute scripts is to add the following statement at the beginning of the scripts file


The general script file (. sh) is this usage. This method first enables the new Sub-shell (the new subprocess), and then executes the command under it.

Another method is the source command mentioned above, which no longer produces a new shell, but executes all commands under the current shell.


SOURCE command dot (.) Command.

Under Bash, enter man source and find the Source command interpretation where you can see the explanation "read and execute commands from filename in the"

Current shell environment and ... ". As you can see, the source command executes individual commands in the parameter file in the current process, not another screwdriver

Process (or Sub-shell).


Enter man exec under bash and find the EXEC command interpreter where you can see the "No new process is created." Such an explanation, that is to say exec's life

The order does not produce a new child process. So what is the difference between exec and source?

When executed, the EXEC command closes the current shell process and then continues execution with the subsequent command.

1. The system call exec is a new process to replace the original process, but the process of the PID remains unchanged. Therefore, it can be argued that the exec system call did not create a new

process, just replacing the contents of the original process context. The code snippet, data segment, and stack segment of the original process are replaced by the new process.

A process consists mainly of the following elements:

(1) A procedure that can be executed

(2) All data associated with the process (including variables, memory, buffers)

(3) Program context (program counter PC, save program execution location)

2. Exec is a function cluster, consisting of 6 functions, which begin with excl and EXECV respectively.

Execute the EXEC system call, generally, to create a new process with the fork () function, and then let the process execute the EXEC call. We know that in fork () established

After the new process, the parent shares the code snippet with the child process, but the data space is separate, but the parent process will copy the contents of its data space into the subprocess, and the

The following will also be copy to the subprocess. For efficiency, a write-time copy strategy that does not copy the parent process's address space when creating a subprocess

The parent-child process has a common address space, which replicates the address space when the child process needs to write data, such as writing to the buffer, and replicates the buffer to the child

Process. The parent-child process thus has a separate address space. For fork () after Exec, this strategy can improve efficiency, if the first

Copy, after exec, the child process data is discarded and replaced by a new process.

3. The difference between exec and system

(1) Exec is directly with the new process to replace the original program to run, after the operation is not back to the original program.

(2) system is to invoke the shell to execute your command, system=fork+exec+waitpid, after execution, go back to the original program. Continue to execute the following


In short, if you call with Exec, you should first fork a new process and then exec. and system does not need you to fork the new process, has been encapsulated well.

exec I/O Redirect and application examples

1, the basic concept (this is to understand the knowledge behind the premise, please be sure to understand)

A, I/O redirection is usually related to FD, Shell FD is usually 10, namely 0~9;

b, there are 3 commonly used FD, 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error output), default and keyboard, monitor

, monitor related;

C, to change the sent out data channel (stdout, stderr), so that the output to the designated file;

E, 0 is the same as the 1>;

F, in IO Redirect, stdout and stderr pipeline will be ready before the data from stdin read;

G, Pipeline "|" (Pipe line): The stdout of the previous command receives the stdin of the next command;

H, tee command is without affecting the original I/O, the stdout copies of a copy to the file;

I, Bash (Ksh) procedure to execute a command: Parse command-variable Evaluation-command substitution (' and $ ())-redirect-wildcard expansion-Determine path-execute command;

J, () put command group Sub-shell to execute, also known as nested Sub-shell, it has a very important feature is: Inherit the parent shell

Standard input, output, and error plus any of the other open file descriptors.

K, EXEC command: Often used to replace the current shell and restart a shell, in other words, did not start the child shell. When using this command, any current

The environment will be cleared. When exec operates on a file descriptor, it is only then that EXEC does not overwrite your current shell environment.

2. CMD &n uses system call DUP (2) to copy file descriptor N and use the result as standard output

&-Turn off standard output

n&-indicates that n-number output is closed

All of these forms can lead to a number, where the file descriptor is specified by this number instead of the default 0 or 1. Such as:

... 2>file runs a command and directs the error output (file descriptor 2) to file.

... 2>&1 runs a command and merges its standard output and output. (Strictly speaking, by copying the file descriptor to create a file descriptor of 2, but the effect

Typically, two streams are merged. )

We have a detailed description of 2>&1:2>&1 is fd2=fd1, this is not to say that FD2 value equals FD1 value, because > is to change the sent out of the data letter

means to change the FD2 "data output channel" to FD1 "data output channel". If this is the case, the change does not seem to have any effect

The default output for FD2 and the default output for FD1 are all monitor, the same!

However, when FD1 is another file, or even other FD, this has a special purpose. Please be sure to understand this.

3, if stdin, stdout, stderr was redirected or closed, but did not save the original FD, you can restore it to the default state?

If stdin is turned off, because it causes an exit, it must not be restored.

If you redirect or close stdout and stderr one of them, you can recover because they are sent to monitor by default (but do not know if there are any other effects). Such as

Restores the redirected or closed stdout:exec 1>&2 and restores the redirected or closed stderr:exec 2>&1.

If stdout and stderr are all closed, and do not save the original FD, you can use: Exec 1>/dev/tty recovery.

4, cmd >a 2>a and cmd >a 2>&1 why different?

CMD >a 2>a:stdout and stderr are sent directly to file A, a file is opened two times, resulting in stdout and stderr covering each other.

CMD >a 2>&1:stdout sent directly to file A, stderr is inherited FD1 pipeline, and then sent to file a. A file is only opened once, is FD1

To open it.

I think: the difference between them is:

CMD >a 2>a is equivalent to the use of two competing pipelines using file A;

and cmd >a 2>&1 only use a pipeline, but at its source already included stdout and stderr.

In terms of IO efficiency, cmd >a 2>&1 efficiency should be higher!

EXEC 0exec 1>outfilename # Open File Outfilename as stdout

EXEC 2>errfilename # Open file Errfilename as stderr

EXEC 0&-# Close FD1

EXEC 5>&-# Close FD5

[Linuxidc.com@linuxidc.com shell]$ Cat 1

11 22 33 44 55

66 22 33 11 33

324 25 63 634 745

[Linuxidc.com@linuxidc.com shell]$ Cat 2

> 1.txt

EXEC 4<&1

EXEC 1> 1.txt

While Read line


Echo $line

Done < 1

EXEC 1<&4

EXEC 4>&-

[linuxidc.com@linuxidc.com shell]$ Sh./2

[Linuxidc.com@linuxidc.com shell]$ Cat 1.txt

11 22 33 44 55

66 22 33 11 33

324 25 63 634 745

[Linuxidc.com@linuxidc.com shell]$

Modify the time format for LS display:

Ls-l--time-style ' +%y/%m/%d%h:%m:%s '
Total 0
-rw-r--r--1 root 0 2008/08/01 12:23:06 file1
-rw-r--r--1 root 0 2008/08/01 12:23:06 file2

--time-style can also exist in environment variables:

Export time_style= ' +%y/%m/%d%h:%m:%s '

Total 0
-rw-r--r--1 root 0 2008/08/01 12:23:06 file1
-rw-r--r--1 root 0 2008/08/01 12:23:06 file2

Internal commands and external commands in Linux:
Unix commands are divided into internal commands and external commands. Internal commands are actually part of a shell program that contains some of the more concise UNIX system commands that are identified by the shell program and run inside the shell program, typically loaded and hosted in system memory when the UNIX system loads the runtime. External commands are part of the utilities in UNIX systems, because the utilities are usually powerful, so they contain a large amount of programs that are not loaded into memory with the system when they are loaded, but are redeployment to memory when they are needed. Typically, an entity of an external command is not included in the shell, but its command execution process is controlled by the shell. The shell program manages the path lookup, loading and storage of the external command execution, and controls the execution of the command.
After you understand internal commands and external commands, you may have questions. So how do I see if a command is an internal command or an external command?

1, in the terminal input Man builtins, you can pull out the outline of the internal command and interpretation.

2, use the command to enable the transfer out, the latter will print out the internal command

[Root@localhost shell]# Enable LS

Bash:enable:ls:not a shell builtin

1. http://blog.sina.com.cn/s/blog_6238358c0100sg5n.html

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.