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.

global _start
 
: syscall ( num:eax -- result:eax ) syscall ;
 
: exit ( status:edi -- noret ) 60 syscall ;
: bye ( -- noret ) 0 exit ;
 
: write ( buf:esi len:edx fd:edi -- ) 1 syscall drop ;
 
1 const stdout
 
: print ( buf len -- ) stdout write ;
 
: newline ( -- ) s" \n" print ;
: println ( buf len -- ) print newline ; 
 
: find0 ( start:rsi -- end:rsi ) lodsb 0 cmp latest xne ; 
: cstrlen ( str:rdi -- len:rsi ) dup find0 swap sub dec ;
: cstr>str ( cstr:rdx -- str:rsi len:rdx ) dup cstrlen xchg ;
 
: print-arg ( arg -- ) cstr>str println ;
 
: arg0 ( rsp -- rsp ) 8 add @ ; inline
 
: _start ( rsp -- noret ) arg0 print-arg bye ;