Search code examples
shellunixshebang

What is the practical value of using a shebang line?


I don't notice any difference when I call a script with say source or ruby. I'm guessing the benefit of a shebang line is that the environment knows what program to invoke if you're running the script as an executable. Not sure if this is the case; but I'm just verifying.


Solution

  • The source command ignores the shebang line. When a script is invoked by shell in the normal way (not with . or source), shell uses the shebang line to fork the right interpreter for the script. The shebang line can contain:

    • direct path to any executable that can interpret the script, along with arguments to the interpreter (#!/usr/local/bin/perl -w)
    • /usr/bin/env that would find the path (#!/usr/bin/env bash). The advantage of this is that it prevents the hardcoded path and lets the shell pick up the environment specific path. In this case, there is no way to send an argument to the interpreter

    See also: