Why does Bash run scripts in subshells?

Lavya

Why does the Bash shell run scripts in subshells? What is the advantage of doing so?

Isaac

The shell, well, initially called the "user interface" of a system, was given the responsibility of executing programs (a.k.a tasks). To call a task, the shell would, in turn, ask the kernel to execute the task. The kernel manage the memory the task would use and the permissions to read or write to files. To ask the kernel to "execute" a program, the basic method is to fork a new task (which gives it a new PID (process number)) and then to exec the new program inside that new PID. The kernel would receive a list of arguments:

int execve(const char *filename, char *const argv[], char *const envp[]);

Basically asking the kernel to execute filename with arguments argv[]. The kernel does what is asked to do and when the program terminates the control returns to the parent process.

Taking the shell as the executor of programs, it is an obvious extension that it could also execute text files that some interpreter could understand. That is the mechanism of the shebang #! /interpreter which the kernel also understand.

So, a shell could (and sometimes do) "execute an script", but the most natural execution sequence is to ask the kernel to do as with any other program: load the program and give it control of the process (PID).

It is expected that any program executed inside a different PID doesn't pollute the parent PPID. That is: changes in one PID doesn't affect the parent PID.

So, when "an script" gets executed it (usually) gets a new PID. Whether it is called subshell or a child shell is sometimes confusing, but what matters is that it runs inside a different PID. Usually a child process (PID).

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to run the bash scripts file?

분류에서Dev

Wait for Bash process substitution subshells

분류에서Dev

How do I run bash shell scripts in cygwin?

분류에서Dev

How do run MYSQL scripts from a shell script with bash variables?

분류에서Dev

Why does bash link to ncurses?

분류에서Dev

Why does powershell freeze for a bit when running my python scripts

분류에서Dev

Why does this for loop run into an ArrayIndexOtOfBounds error? (Java)

분류에서Dev

why does the mv command in bash delete files?

분류에서Dev

Why does my bash script hang?

분류에서Dev

BASH scripts selectively working

분류에서Dev

Why can not the Bash script file run by shortcut get the content of a file?

분류에서Dev

Why does a Ubuntu guest in VirtualBox run very, very slowly?

분류에서Dev

Why does ssh look for keys in /root/.ssh when run with sudo?

분류에서Dev

Why application does not run from SCREEN without delay?

분류에서Dev

Why does kill -9 0 on a mac simply kill the bash shell?

분류에서Dev

Why does bash need && to echo a variable on one line?

분류에서Dev

Why does Bash give "No such file or directory" for a program that's in my PATH?

분류에서Dev

Why does alias in /etc/bash.bashrc not apply to sudo?

분류에서Dev

Why does "sbt run" fail with OutOfMemoryError while "activator run" works fine?

분류에서Dev

On `fork`, children processes, and "subshells"

분류에서Dev

How to run programs and scripts NOT in the background on a remote machine?

분류에서Dev

PHP scripts aren't run on localhost

분류에서Dev

Checking host availability by using ping in bash scripts

분류에서Dev

Reading passwords without showing on screen in Bash Scripts

분류에서Dev

Writing a bash function to autoload matlab scripts

분류에서Dev

Why does my vixie cron entry to run every other day, actually run on consecutive days every 4th time?

분류에서Dev

Why does jQuery's .animate() run the "complete" function before it completes the animation?

분류에서Dev

while tryying to run 'rails s', why does it fail and ask to INSTALL gem 'therubyracer' to use Less

분류에서Dev

Why does the below code run without an error when both overlapping fields are used?

Related 관련 기사

  1. 1

    How to run the bash scripts file?

  2. 2

    Wait for Bash process substitution subshells

  3. 3

    How do I run bash shell scripts in cygwin?

  4. 4

    How do run MYSQL scripts from a shell script with bash variables?

  5. 5

    Why does bash link to ncurses?

  6. 6

    Why does powershell freeze for a bit when running my python scripts

  7. 7

    Why does this for loop run into an ArrayIndexOtOfBounds error? (Java)

  8. 8

    why does the mv command in bash delete files?

  9. 9

    Why does my bash script hang?

  10. 10

    BASH scripts selectively working

  11. 11

    Why can not the Bash script file run by shortcut get the content of a file?

  12. 12

    Why does a Ubuntu guest in VirtualBox run very, very slowly?

  13. 13

    Why does ssh look for keys in /root/.ssh when run with sudo?

  14. 14

    Why application does not run from SCREEN without delay?

  15. 15

    Why does kill -9 0 on a mac simply kill the bash shell?

  16. 16

    Why does bash need && to echo a variable on one line?

  17. 17

    Why does Bash give "No such file or directory" for a program that's in my PATH?

  18. 18

    Why does alias in /etc/bash.bashrc not apply to sudo?

  19. 19

    Why does "sbt run" fail with OutOfMemoryError while "activator run" works fine?

  20. 20

    On `fork`, children processes, and "subshells"

  21. 21

    How to run programs and scripts NOT in the background on a remote machine?

  22. 22

    PHP scripts aren't run on localhost

  23. 23

    Checking host availability by using ping in bash scripts

  24. 24

    Reading passwords without showing on screen in Bash Scripts

  25. 25

    Writing a bash function to autoload matlab scripts

  26. 26

    Why does my vixie cron entry to run every other day, actually run on consecutive days every 4th time?

  27. 27

    Why does jQuery's .animate() run the "complete" function before it completes the animation?

  28. 28

    while tryying to run 'rails s', why does it fail and ask to INSTALL gem 'therubyracer' to use Less

  29. 29

    Why does the below code run without an error when both overlapping fields are used?

뜨겁다태그

보관