There are three ways to run a program in Linux, namely:
1. Make the file have executable permissions and run the file directly.
The chmod command is used to modify the permissions of files. x gives the file executable permissions. Just like we ran the program above. But the results we saw were somewhat different from what we imagined.
2. Directly call the command interpreter to execute the program.
As shown in the figure:
Since our interpreter is /bin/sh, we use the sh command interpreter to execute the program.
(The current working path has not changed)
(Recommended tutorial:linux tutorial)
3. Use source to execute the file.
As shown in the picture:
(The current working path has changed)
Three ways to run the shell program , the execution process of the first two methods is as follows:
(1) The parent process receives the command, and then finds that it is not a built-in command, so it creates a shell process like itself to execute the external command
(2) This shell sub-process replaces itself with /bin/sh, and the sh process sets its own running environment variables, including the $PWD variable.
(3) The sh process executes the built-in commands cd and echo in sequence. During this process, the environment variables of the sh process (child process) are changed by the cd command.
(4) After the child process is executed, it dies. The parent process that has been waiting wakes up and continues to accept commands.
Analysis:
The current directory (environment variable) of the parent process cannot be changed by the child process. However, when using source to execute a shell script, the child process will not be created, but will be executed directly in the parent process.
Recommended related video tutorials:linux video tutorial
The above is the detailed content of How to run shell program in linux. For more information, please follow other related articles on the PHP Chinese website!