How to call another script in shell script

Source: Internet
Author: User

Developed on the Linux platform, another script file is often executed on the console. The following methods are frequently used:./My. ShOrSource my. ShOr. My. Sh; What are the differences between the three methods? Let's take a look at how to call another shell script in a shell script. The methods include:Fork exec source.

1. Fork (/directory/script. Sh ):

If the shell contains the execution command, the sub-command does not affect the parent-level command. After the sub-command is executed, the sub-command is executed again. Child-level environment variables do not affect the parent-level.

Fork is the most common, that is, directly using/directory/script in the script. sh to call the script. sh script. run a sub-shell script to execute the call. When Sub-shell is executed, parent-shell is still running.

After sub-shell is executed, the system returns parent-shell. Sub-shell to inherit environment variables from parent-shell. However, the environment variables in sub-shell will not be brought back to parent-shell.

2. Exec (exec/directory/script. Sh ):

After a sub-level command is executed, the parent-level command is no longer executed.

Unlike fork, exec does not require a new sub-shell to execute the called script. The called script and the parent script are executed in the same shell. However, after exec is used to call a new script, the content after the exec line in the parent script will not be executed again. This is the difference between exec and source.

3. Source (Source/directory/script. Sh ):

After executing the sub-level command, continue to execute the parent-level command. At the same time, the environment variables set for the sub-level will affect the environment variables of the parent level.

The difference with fork is that instead of opening a sub-shell to execute the called script, it is executed in the same shell. therefore, the variables and environment variables declared in the called script can be obtained and used in the main script.

The above three methods are different for calling shell scripts ,. /My. SH is the fork method, source my. SH and. my. SH is both a source method.

In Linux, to build an embedded development platform, you must execute scripts to set environment variables before cross-compiling code. Remember to execute shell scripts in sourc or vertex mode. The reason is as follows.

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.