The request starts with execve
A script starts the same way as any other program. The shell forks a child process, and in the child it calls execve with the path of the file and the argument list. The execve system call replaces the memory image of the calling process with a new program. The process ID does not change. The process that becomes the script keeps the PID that fork gave it, which is why the shell and the script share a process identity in the process list.
The kernel does not run every file in the same way. Before it loads a program, it checks the start of the file to choose a handler. An ELF binary is recognized by its magic bytes. A file that starts with the two characters #! is handled by the script loader. That loader reads the first line of the file, which is a short piece of text, and it refuses to continue if the line is too long for the buffer it reads.
Recognizing the #! prefix
The script loader splits the rest of the first line into an interpreter path and at most one optional argument. Leading and trailing spaces are dropped. The interpreter path ends at the first space, and the optional argument is the text that follows, up to the end of the line. That text is passed as one string. It is not split again, which matters for the rules that come next.
For the line #!/bin/bash and a call such as ./hello.sh first second, the kernel builds the argument list for the interpreter. The list holds the interpreter path, then the script path, then the remaining arguments. The kernel does not execute the script text itself. It starts bash with the script path as its first argument. In the process list, the command line reads /bin/bash ./hello.sh first second, which is why ps shows the interpreter name.
If the shebang has an optional argument, as in #!/bin/bash -e, the kernel places that argument between the interpreter and the script path. The list becomes bash, then the option, then the script path, then the rest of the arguments. Linux supports this one extra argument and no more.
What the shebang line cannot do
The line #!/usr/bin/env bash -e looks as if it passes two options to bash. On Linux it does not work as intended. The kernel passes everything after the path as one argument, so env receives the single argument bash -e. Env then looks for a program whose name contains a space, and it fails with an error that names bash -e. The fix is to drop the option and put set -e inside the script, or to use an absolute interpreter path with a single argument.
Two more limits follow from the same design. The interpreter path must be a real path that the kernel can open. The line should be short, because the loader reads only the start of the file. A file with Windows line endings leaves a carriage return at the end of the interpreter path. That produces the message no such file or directory, even though the file exists, which is a confusing result for a common mistake.
Scripts with no shebang
If a file has the execute bit but no #! prefix, execve returns the error ENOEXEC, which means that the format is not recognized. The kernel has done nothing else at that point, so the program is not started. Bash then takes over. It runs the file itself as a shell script in a new child process, which is why a file without a shebang can still work when you run it with ./ in bash. Other shells may treat such a file differently, and that is a good reason to write a shebang in every script.
The error names are worth knowing. ENOENT means the path does not exist, and it is the source of the message no such file or directory. EACCES means the permission bits do not allow execution, and it is the source of Permission denied and status 126. ENOEXEC is the unrecognized-format case. The shell turns these outcomes into the standard exit statuses 126 and 127 that the earlier units described.
Seeing it with strace
The strace tool records the system calls that a process makes. Filtering the output to execve shows the handoff directly. The command below traces one script run, and it assumes that hello.sh exists and is executable:
strace -e trace=execve ./hello.shThe first line of the report is the execve call for the script path. The kernel loads the interpreter inside that same system call, so the line names ./hello.sh and not /bin/bash. The interpreter then runs under the same process. The last line reports the exit status of the process.
To see the interpreter name while a long script runs, use ps -o args in a second terminal, which shows the command line that the kernel built. strace writes its report to standard error, so use 2>&1 when you pipe the output to another command. The report is the most direct evidence of what the kernel did, and it is worth reading once.