When writing the shell script to find that the CD switch directory can not switch, the code is the following.
1 #!/bin/bash2#changedir. SH3history4 cd/home/ Firefox516 pwd
I think carefully, I carry out the time is. Changedir.sh to execute, so that the terminal will produce a child shell, the child shell to execute my script, in the child shell has switched the directory, but once the child shell execution, immediately exit, the child shell variables and operations are all retracted. Back to the terminal, there is no change in the process at all.
So how do you verify the above conjecture? Look at the code below.
1 #!/bin/bash 2 #changedir. Sh 3 History 4 Cd/home/firefox 5 Sleep 1 6 pwd
First, according to the./changedir.sh execution, this time the terminal does not switch directories, the result of history execution is empty, stating that there is no historical command in the child shell (this is certain).
Then follow the source changedir.sh execution, this time is directly in the terminal shell execution script, did not generate a child shell, the result is the output history command, and switch directories.
The use of the CD command in the shell script