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.

 
10 GOSUB 40"GET PROGRAM NAME
20 PRINT N$
30 END
 
40 REMGET PROGRAM NAME
50 GOSUB 100"GET INPUT BUFFER
60 GOSUB 200"REMOVE RUN PREFIX
70 GOSUB 300"REMOVE , SUFFIXES
80 GOSUB 400"TRIM SPACES
90 RETURN
 
100 REMGET INPUT BUFFER
110 N$ = ""
120 FOR I = 512 TO 767
130     B =  PEEK (I) - 128
140     IF B < 32 THEN  RETURN
150     N$ = N$ +  CHR$ (B)
160 NEXT I
170 RETURN
 
200 REMREMOVE RUN PREFIX
210 P = 1
220 FOR I = 1 TO 3
230     FOR J = P TO LEN(N$)
240         C$ =  MID$ (N$,J,1)
250         P = P + 1
260         IF C$ = " " THEN  NEXT J
270         IF C$ = MID$("RUN",I,1) THEN NEXT I:N$ =  MID$(N$,P,LEN(N$)-P+1):RETURN
280 PRINT "YOU NEED TO RUN THIS PROGRAM USING THE  RUN COMMAND FROM DOS."
290 END
 
300 REMREMOVE , SUFFIXES
310 L =  LEN (N$)
320 FOR I = 1 TO L
330     C$ =  MID$ (N$,I,1)
340     IF C$ = "," THEN N$ =  LEFT$(N$,I - 1): RETURN
350 NEXT I
360 RETURN
 
400 REMTRIM SPACES
410 GOSUB 600
 
500 REMLEFT TRIM SPACES
510 L = LEN(N$) - 1
520 FOR I = L TO 0 STEP -1
530     IF I < 0 THEN RETURN
540     IF LEFT$ (N$,1) <> " " THEN RETURN
550     IF I THEN N$ = RIGHT$ (N$, I)
560 NEXT I
570 N$ = "
580 RETURN
 
600 REMRIGHT TRIM SPACES
610 L = LEN(N$) - 1
620 FOR I = L TO 0 STEP -1
630     IF I < 0 THEN RETURN
640     IF RIGHT$ (N$,1) <> " " THEN RETURN
650     IF I THEN N$ = LEFT$ (N$, I)
660 NEXT I
670 N$ = "
680 RETURN
 