Analysis of the Influence of source and. Comma commands on the current parent shell in Shell

Source: Internet
Author: User

 

Lu1 (){
Echo 'aaaaaaaaa'
}

Lu2 (){
Echo 'bbbbbbbbbbbb'
}
The above is the source code of run. Sh. We can see that there are only two function definitions and there is no place to call these two functions. If we execute these two functions like this:
Luther @ gliethttp :~ $./Run. Sh
Luther @ gliethttp :~ $
We can see that there is nothing, because there is no place to call these two functions. We hope that in our Luther @ gliethttp :~ $ Call these two functions at the end of the prompt to try,
Luther @ gliethttp :~ $ Lu1
Bash: lu1: Command not found
Luther @ gliethttp :~ $ Lu2
Bash: lu2: Command not found
Luther @ gliethttp :~ $
The results all showed that the command could not be found. Isn't shell our requirement not yet supported? Of course not. Such a useful requirement shell has already prepared a solution for us to see how to implement it,
There are two commands in shell to complete our work. One is source and the other is the. Comma operator equivalent to source:
Luther @ gliethttp :~ $. Run. Sh or Luther @ gliethttp :~ $ Source run. Sh
In this case, run. the variables and functions created in the sh script are added to the execution. or source shell, until the shell window is closed, otherwise run. the variables and functions created in SH will always be valid,
If we only execute. /run. sh starts a subshell to run our run. sh script, then run. the variables and functions in the sh script will closely affect the subshell started by the shell. /run. run. the life of SH variables and functions has reached the end.
Let's take a look at the current phenomenon:
Luther @ gliethttp :~ $./Run. Sh
Luther @ gliethttp :~ $ Lu1
Bash: lu1: Command not found
Luther @ gliethttp :~ $ Lu2
Bash: lu2: Command not found
Luther @ gliethttp :~ $. Run. Sh
Luther @ gliethttp :~ $ Lu1
Aaaaaaaaa
Luther @ gliethttp :~ $ Lu2
Bbbbbbbbb
Luther @ gliethttp :~ $
Restart a new shell:
Luther @ gliethttp :~ $./Run. Sh
Luther @ gliethttp :~ $ Lu1
Bash: lu1: Command not found
Luther @ gliethttp :~ $ Lu2
Bash: lu2: Command not found
Luther @ gliethttp :~ $ Source run. Sh
Luther @ gliethttp :~ $ Lu1
Aaaaaaaaa
Luther @ gliethttp :~ $ Lu2
Bbbbbbbbb
Luther @ gliethttp :~ $

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.