$_ CLI In Depth
Learning progress 0%
case scripting exit-codes control-flow

System Deep-Dive: How the Kernel Runs a #! Script

5 min read Module 13: Scripting Basics & Exit Codes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:29

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:

BASH1 lines
strace -e trace=execve ./hello.sh

The 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.

How the kernel runs a script

Visualizer

A script is only a file. The kernel decides how to run it from its first bytes: a #! line names an interpreter, and a file without one falls back to the shell.

1 / 5
execve() on ./hello.sh
kernel reads the first bytes
sees #! and parses the line
argv rebuilt, interpreter started
bash reads the script
$ ./hello.sh a b

You type ./hello.sh a b. Bash forks a child, and the child calls execve() on the file, passing the words as argv.

printf '#!/bin/bash\necho "argv0=$0 args=$*"\n' > /tmp/shebang-demo.sh; chmod +x /tmp/shebang-demo.sh; /tmp/shebang-demo.sh a b
Check yourself Question 1 of 4
Concept Check System call that starts a program recall

Which system call makes the kernel load the program named by a script file, after the shell has forked a child?

Recap & Key Takeaways

A script is started by execve, which replaces the process image without changing the PID, and the kernel script loader handles files that begin with #!.
The kernel passes the interpreter, at most one optional argument, the script path, and the remaining arguments to the interpreter.
A shebang such as #!/usr/bin/env bash -e fails on Linux, because the whole rest of the line becomes one argument.
A file with no shebang returns ENOEXEC from execve, so bash runs it itself, while a missing path or missing execute bit produces statuses 127 and 126.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue