Linux in source, SH, bash,./What's the difference?

Source: Internet
Author: User

In Linux, source, SH, bash,./can execute shell script files, what's the difference?

-----------

1. Source

SOURCE a.sh

Reads, executes a.sh in the current Shell , and a.sh does not need to have " Execute permission "

The source command can be abbreviated as "."

. a.sh

Note: There are spaces in the middle.

2, Sh/bash

SH A.shbash a.sh

is to open a subshell to read, execute a.sh, and a.sh do not need to have " Execute permission "

It is common to set variables in scripts running in Subshell without affecting the parent shell.

3,./

./a.sh#bash:./a.sh: Insufficient permissions chmod +x a.sh./a.sh

Open a Subshell to read, execute a.sh, but a.sh need to have " Execute permission "

You can add execute permissions with chmod +x

4. Fork, source, exec

    • When you run a script using fork, the shell (parent process) produces a child process to execute the script, and when the child process finishes, it returns to the parent process, but the parent The process environment is not changed by the change of child process.
    • When you run a script using source, the script executes within the current process, rather than creating a child process. As all execution results are completed within the current process, if the context of the script changes, it will certainly change the current process environment.
    • When you run script with exec, it is the same as source, where script executes within the current process, but the remainder of the original code within the process is terminated. Similarly, the environment within the process changes as the script changes.

Usually, if we do, it is the default fork.

For the sake of practice, we can build 2 sh files first, the following code comes from Chinaunix's network person:

1.sh

#!/bin/basha=becho "PID for 1.sh before exec/source/fork:$$" Export Aecho "1.sh: \ $A are $A" case $ in    exec)        echo "U Sing exec ... "        exec./2.sh;;    SOURCE)        echo "using Source ...".        /2.sh;;    *)        echo "Using fork by default ..."        ./2.sh; Esacecho "PID for 1.sh after exec/source/fork:$$" echo "1.sh: \ $A is $A"

2.sh

#!/bin/bashecho "PID for 2.sh: $$" echo "2.sh get \ $A = $A from 1.sh" A=cexport aecho "2.sh: \ $A is $A"

Run it yourself and watch the results:)

chmod +x 1.shchmod +x 2.sh./1.sh fork./1.sh source./1.sh exec

Linux in source, SH, bash,./What's the difference?

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.