Tag: exec source Fork
Fork is a system call to Linux that creates a child process that is a copy of the parent process, obtains a certain resource allocation from the parent process, and inherits the environment of the parent process. The only difference between a child process and the parent process is the PID (process ID). Environment variables (variables passed to child processes, hereditary is the fundamental difference between local variables and environment variables) can only be passed from the parent process to the child process in one direction. Regardless of how the environment variable of the child process changes, the environment variables of the parent process are not affected. The commands in the default shell are this way, create Sub-shell, and then execute. After execution, return to the parent shell.
Both exec and source are part of Bash's built-in commands, and no new processes are generated during the execution of source and exec, and the script being invoked executes within the same shell process as the parent script. The difference between source and exec is that after exec invokes a new script, the content after the Exec line in the parent script is no longer executed. So the variables and environment variables declared in the invoked script can be obtained and used in the main script.
Summarize:
SOURCE command: Do not create child processes, execute scripts in the current shell process,
EXEC command: Do not create a child process, execute script in the current shell process, the content after the Exec line in the parent script does not execute
Fork is a system call, a child process is created, the parent process blocks waiting for the child process to finish executing, and then continues to execute
The difference between source/fork/exec in the shell