Tracing the #!: How the Linux Kernel Handles the Shebang
1 min read
Summary
Using the shebang (#!) at the beginning of a script allows easy use of scripts in Bash, zsh or other shells.
This is not interpreted by the shell, but by the kernel, starting with the execve syscall in the Linux kernel.
This analyse the binary programme format, which for shell scripts, leads to binfmt_script.c.
Using the binfmt_misc.c source file offers an interesting detour to understand more about the use of magic byte sequences to achieve a similar effect to the shebang.
The difference between using a shebang and running a script through a shell is also discussed, along with the point in which execute permission is checked in a shebang-ed script.
The shebang can be used to write a script to automate almost anything.
Using #!/use/bin/env THEAPP allows the rest of the file to be sent as an argument to the application and executed.