There are a few steps that happen when a shell runs a self contained script in a UNIX environment.

Shell

First, the shell asks the Unix kernel to start a new process and run the given program.

Kernel

The kernel knows how to start processes for compiled programs. If it is not a compiled program, the kernel will fail and return an error.

Shell

The shell sees the error and realizes that this must be a shell script. Then it starts a new copy of /bin/sh/ (the standard shell) to run the program.

Kernel

Qbert character with the characters '#!' displayed in a cloud bubble above their head'

The kernel checks the script file for a special first line that begins with two characters “#!”. This line contains the full pathname of an interpreter to use to run the program.

This makes it simple to invoke any interpreted language such as a standalone awk program.