Tuomas Toivonen Tuomas Toivonen - 1 year ago 56
Bash Question

Is linux shell just optional utility or mandatory when running processes

I'm still pretty confused with the role of linux shell running programs despite of using linux a lot.

I understand there are two type of shells, interactive shells and non-interactive shells. Terminal session interacts with interactive shell, and scripts run in non-interactive shell. But is there really other difference than ability to read input and print output? If I invoke script from shell, does it run in this interactive shell or new non-interactive shell inside shell?

Also, when I execute binary either by invoking it through interactive shell or graphical interface, does it always run in the shell, or could a process run without shell at all? It's said that all processes communicates with kernel through the shell, but I'm confused because in docker, you can define the entrypoint to be either a binary or "sh -c binary".

Answer Source

The shell is just one possible interface. Every Linux system has a notion of a "first" process (usually called init) that is started directly by the kernel. Every other program on your computer is started by another process that first forks itself, then calls exec (actually, one of about 6 functions in the same family) to replace itself with a different program.

The shell is just one possible interface, one that parses text into requests to run other programs. The shell command line mv foo bar is parsed as a request to pass fork the shell and call exec in the new copy with the three words mv, foo, and bar as arguments.

Consider the following snippet of Python:

subprocess.call(["mv", "foo", "bar"])

which basically does the same thing: the Python program forks itself and calls exec with the three given strings as arguments. There is no shell involvement.