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

Source: Internet
Author: User

Use source, dot (.) when the shell script is executed, no sub-processes are generated. The shell script runs in the current shell, variables declared in shell scripts are visible in the current shell.

When the shell script is directly executed using the script file name, a sub-process is generated. The shell script is run in the sub-process. After the shell script is run, variables declared in shell scripts are invisible in the current shell.

Verification process:

The script content of TT. Sh in the current directory is as follows:

Echo $
Ttvar= 12345

1. First, let's look at the current shell PID: 28210
Test @ btdalvm03 :~ /C $ echo $
28210
2. Execute TT. Sh in source mode. The PID printed by the script is the same as the PID of the current shell. The variable ttvar defined in TT. Sh can still be accessed after the script is executed.

Test @ btdalvm03 :~ /C $ source TT. Sh
28210
Test @ btdalvm03 :~ /C $ echo $ ttvar
12345

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 ttvar
Test @ btdalvm03 :~ /C $ echo $ ttvar

Test @ btdalvm03 :~ /C $. tt. Sh
28210
Test @ btdalvm03 :~ /C $ echo $ ttvar
12345
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 ttvar
Test @ btdalvm03 :~ /C $ echo $ ttvar

Test @ btdalvm03 :~ /C $ TT. Sh
28796
Test @ 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.

test@btdalvm03:~/c$ echo $$28210test@btdalvm03:~/c$ echo $$28210test@btdalvm03:~/c$ shsh-3.2$ echo $$29152sh-3.2$ bashbash interactive changedtest@btdalvm03:~/c$ echo $$29153test@btdalvm03:~/c$ ps  PID TTY          TIME CMD28210 pts/1    00:00:00 bash29152 pts/1    00:00:00 sh29153 pts/1    00:00:00 bash29205 pts/1    00:00:00 pstest@btdalvm03:~/c$ exitexitsh-3.2$ echo $$29152sh-3.2$ exitexittest@btdalvm03:~/c$ echo $$28210test@btdalvm03:~/c$
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.