The task is to programmatically obtain the name used to invoke the program. (For example determine whether the user ran "python hello.py", or "python hellocaller.py", a program importing the code from "hello.py".)

Sometimes a multiline shebang is necessary in order to provide the script name to a language's internal ARGV.

See also Command-line arguments

Examples from GitHub.

#light (*
	exec fsharpi --exec $0 --quiet
*)
 
let scriptname =
    let args = System.Environment.GetCommandLineArgs()
 
    let arg0 = args.[0]
 
    if arg0.Contains("fsi") then
        let arg1 = args.[1]
        if arg1 = "--exec" then
            args.[2]
        else
            arg1
    else
        arg0
 
let main =
    printfn "%s" scriptname