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.

":";exec lein exec $0 ${1+"$@"}
":";exit
 
(ns scriptname
  (:gen-class))
 
(defn -main [& args]
  (let [program (first *command-line-args*)]
    (println "Program:" program)))
 
(when (.contains (first *command-line-args*) *source-path*)
  (apply -main (rest *command-line-args*)))