Shell Source command

Source: Internet
Author: User

Http://www.xxlinux.com/linux/article/development/shell/2006-09-22/4499.html

Http://www.diybl.com/course/6_system/linux/Linuxjs/20071027/80454.html

Http://linux.chinaunix.net/techdoc/system/2008/09/04/1030374.shtml


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 implemented through system calls or independent programs, such as SED and awk. Internal commands are implemented by special file formats (. Def), such as CD, history, and exec.

Before explaining the differences between EXE and source, 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 ).


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.



**************************************** *****


Source command usage:

Source filename

Purpose: read and execute the command in filename in the current bash environment.

Note: This command is usually replaced by the "." command.

For example, source. bash_rc and. bash_rc are equivalent.


For example, if you run export $ KKK = 111 In a script, if you run the script with./A. Sh, after the script is executed, you run echo $ KKK

No value is found. If you use source for execution, Then ECHO

Kkk = 111. Because the./A. Sh command is called to run the shell in a sub-shell, the structure is not reflected in the parent shell after execution,

Different from source, it is executed in this shell, so we can see the result.

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) which is another name of the source. Both commands take a script as the parameter, which will be executed as the current shell environment, that is, a new sub-process will not be started. All variables set in the script will become part of the current shell. Similarly, the variables set in the current script will also be used as the script environment. The source (or point) command is usually used to re-execute the modified initialization file, such as. bash_profile and. profile. For example
Editer and

If the term variable is modified, you can use the source command to re-execute the command in. bash_profile without logging out and logging on again. Like. bash_profile or other similar shell scripts, the file can be executed using the source or dot command without the executable permission.


A wonderful use of the source command


When compiling the core, it is often necessary to repeatedly enter a long string of commands, such

Make mrproper

Make menuconfig

Make Dep

Make clean

Make bzimage

.......

These commands are long and cumbersome. And sometimes it is easy to lose mistakes, wasting your time and energy. If these commands are made into a file and executed in sequence automatically

Users, It is very convenient. You can use the source command to do this. Its function is to treat the content of a file as a shell for execution. First in/usr/src/Linux-

2.4.20 create a file named make_command:

Enter the following content:

Make mrproper &&

Make menuconfig &&

Make Dep &&

Make clean &&

Make bzimage &&

Make modules &&

Make modules_install &&

Cp arch/i386/boot/bzimge/boot/vmlinuz_new &&

CP system. MAP/boot &&

VI/etc/Lilo. conf &&

Lilo-V

After the file is established, after each compilation of the core, you only need to input in the/usr/src/linux-2.4.20

Source make_command

That's all. This file can also be used as a script with only a slight modification. Here is to let everyone understand the usage of source. If you are not using lilo to guide the system, you can remove the last two sentences. Configure your own boot program to guide the new kernel.


Commands in shell programming are sometimes the same as those in C. & Represents and, | represents or. Connect the two commands with &, for example

Make mrproper & make menuconfig

, Indicates that the second command can be executed only after the first command is successfully executed. Commands that have requirements on the execution sequence can ensure that the following commands will not be executed blindly once an error occurs.

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.