Differences between using source, dot (.) in Linux and using script file names to execute shell scripts

Source: Internet
Author: User


Use source, dot (.) in Linux (.) use source, dot (.) when the shell script is executed, no sub-processes are generated. The shell script runs in the current shell,
After the shell script is run, the variables declared in the shell script are visible in the current shell. when the shell script is directly executed using the script file name, a sub-process is generated, and the shell script is run in the sub-process,
After the shell script is run, the variables declared in the shell script are invisible in the current shell.
Verification process: www.2cto.com has the following tt. sh script in the current directory: echo $ ttvar = 123451. Let's first look at the current shell pid: 28210test @ btdalvm03 :~ /C $ echo $282102, execute tt in source mode. sh, the pid printed by the script is the same as the pid of the current shell, in tt. the variable ttvar defined in sh can still be accessed after the script is executed. test @ btdalvm03 :~ /C $ source tt. sh28210test @ btdalvm03 :~ /C $ echo $ ttvar12345 3. Execute the command in the dot mode with the same effect as that in the source mode. Use unset to clear the ttvar variable. test @ btdalvm03 :~ /C $ unset ttvartest @ btdalvm03 :~ /C $ echo $ ttvar test @ btdalvm03 :~ /C $. tt. sh28210 www.2cto.com test @ btdalvm03 :~ /C $ echo $ ttvar12345
4. Run the script file directly. Add the PATH to the current folder (or specify the file name with./tt. sh) test @ btdalvm03 :~ /C $ PATH = $ PATH:. test @ btdalvm03 :~ /C $ unset ttvartest @ btdalvm03 :~ /C $ echo $ ttvar test @ btdalvm03 :~ /C $ tt. sh28796test @ btdalvm03 :~ /C $ echo $ ttvar test @ btdalvm03 :~ /C $
As you can see, this method generates a new sub-process. After the script is run, the variables defined in it are not accessible to the current shell. A sub-process is also generated when the sh is changed. The sh is returned to the sh before the sh is changed through exit. [plain] test @ btdalvm03 :~ /C $ echo $28210 test @ btdalvm03 :~ /C $ echo $28210 test @ btdalvm03 :~ /C $ sh-3.2 $ echo $29152 sh-3.2 $ bash interactive changed test @ btdalvm03 :~ /C $ echo $29153 www.2cto.com test @ btdalvm03 :~ /C $ ps pid tty time cmd 28210 pts/1 00:00:00 bash 29152 pts/1 00:00:00 sh 29153 pts/1 00:00:00 bash 29205 pts/1 00:00:00 ps test @ btdalvm03 :~ /C $ exit sh-3.2 $ echo $29152 sh-3.2 $ exit test @ btdalvm03 :~ /C $ echo $28210 test @ btdalvm03 :~ /C $ author kkdelta

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.