The difference between “.” and “.” / “execution in Linux shell

Time:2020-8-19

At present, the differences are mainly in the scope of environmental variables

1. If you use “.” / “to execute, it can be understood that the program runs in a new shell and does not inherit the value of the environment variable of the current shell. At the same time, if the environment variable in the current shell is changed in the program (do not use export), the environment variable value of the current shell will not be changed.

2. If “. Is used to execute, the program inherits the environment variable in the current shell. At the same time, if the environment variable in the current shell is changed in the program (do not use export), the value of the environment variable in the current shell will also change

Another difference is that “.” can only be used for files with execute permission, while “.” can be promoted temporarily

example:

1. There are the following script files test.sh


# pre value
echo 'pre is :' $VAL_TS
 
# change the value
VAL_TS='world'
 
# post value
echo 'post is :' $VAL_TS

2. Execute the following command in the current shell:

[email protected] :~$ VAL_ TS = Hello 'ා defines the variable val_ TS, whose value is hello
[email protected] :~$ echo $VAL_ TS ා output variable val_ Value of TS 
#Results
hello
 
[email protected] :~$ ./ test.sh          #Execute the script using '. /' test.sh
#Results
Pre is: ා the script does not inherit the Val in the current shell_ Value of TS
Post is: World ා after modification, the value is' world '
 
[email protected] :~$ echo $VAL_ TS ා output Val again in shell_ Value of TS
#Results
Hello ා although the variable Val was changed in test. H_ TS value, but it does not work in the shell
 
[email protected] :~$ .  test.sh          #Use "." to execute the script test.sh
#Results
Pre is: Hello ා script program inherits Val from shell_ TS value
Post is: World ා after modification, the value changes to 'world'
 
[email protected] :~$ echo $VAL_ TS ා output Val in shell_ Value of TS
#Results
Changes in the world ා script are applied to the shell

Summary:

Personal understanding:

1. / “is similar to creating a new shell. Executing the program in the script in this new shell is similar to creating a new child process. However, the child process does not inherit all non export type variables of the parent process, and the creation or modification of non export environment variables in the script will not be fed back to the external calling shell

It inherits the environment variables of the current shell, and the modification of the environment variables in the script can also be fed back to the shell

In this article, we will introduce the differences between “.” and “.” in Linux shell. For more information about shell “.” and “.” and “.” please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!