Program name: Difference between revisions

Add Ecstasy example
(Add Ecstasy example)
 
(46 intermediate revisions by 27 users not shown)
Line 7:
 
Examples from [https://github.com/mcandre/scriptname GitHub].
 
=={{header|11l}}==
<syntaxhighlight lang="11l">:start:
print(‘Program: ’:argv[0])</syntaxhighlight>
=={{header|68000 Assembly}}==
{{works with|NEOGEO}}
The name of the game is stored on the cartridge at memory address $000200. It is exactly 16 characters and is padded with spaces (ASCII 32). Since there is no null terminator after this string, use its size to terminate a printing loop.
 
<syntaxhighlight lang="68000devpac">LEA $000200,A3
JSR PrintString ;(my print routine is 255-terminated and there just so happens to be an FF after the name of the game.)</syntaxhighlight>
 
;Output
[[https://ibb.co/4jNm7TW Ninja Adventure]]
 
=={{header|AArch64 Assembly}}==
 
{{works with|aarch64-linux-gnu-as/qemu-aarch64}}
 
Without built-in CRT, <code>argc</code> and <code>argv</code> are stored in the stack. The format looks like:
 
<syntaxhighlight lang="text">sp+0 = argc
sp+8 = argv[0]
sp+16 = argv[1] ...</syntaxhighlight>
 
Each item of <code>argv</code> is a pointer to a null-terminated 8-bit string.
 
<syntaxhighlight lang="arm_assembly">.equ STDOUT, 1
.equ SVC_WRITE, 64
.equ SVC_EXIT, 93
 
.text
.global _start
 
_start:
stp x29, x30, [sp, -16]!
mov x29, sp
ldr x0, [sp, 24] // argv[0]
bl _strlen // strlen(argv[0])
mov x2, x0
mov x0, #STDOUT
ldr x1, [sp, 24]
bl _write // write(stdout, argv[0], strlen(argv[0]))
ldp x29, x30, [sp], 16
mov x0, #0
b _exit // exit(0);
 
// ssize_t _strlen(const char *str)
_strlen:
mov x1, x0
mov x0, #-1
1: add x0, x0, #1
ldrb w2, [x1, x0]
cbnz x2, 1b
ret
 
.text
//////////////// system call wrappers
// ssize_t _write(int fd, void *buf, size_t count)
_write:
stp x29, x30, [sp, -16]!
mov x8, #SVC_WRITE
mov x29, sp
svc #0
ldp x29, x30, [sp], 16
ret
 
// void _exit(int retval)
_exit:
mov x8, #SVC_EXIT
svc #0</syntaxhighlight>
 
=={{header|Ada}}==
Being a compiled language, Ada has difficulties accessing source code filenames. But it is easy to access the executable's filename, using the function Command_Name defined in Ada.Command_Line:
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line, Ada.Text_IO;
 
procedure Command_Name is
begin
Ada.Text_IO.Put_Line(Ada.Command_Line.Command_Name);
end Command_Name;</langsyntaxhighlight>
 
=={{header|Aime}}==
The program command line arguments are accessible via the argc()/argv() functions. The program name is the first in the list of arguments.
<langsyntaxhighlight lang="aime">o_text(argv(0));
o_byte('\n');</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="text">
BEGIN
print ((program idf, newline))
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 33 ⟶ 104:
$
</pre>
 
=={{header|Amazing Hopper}}==
El primer parámetro en Hopper y sus sabores, siempre será el nombre del programa.
<syntaxhighlight lang="amazing hopper">
#include <hbasic.h>
Begin
GetParam(name File)
Print("My Program name: ", name File,Newl)
End
</syntaxhighlight>
{{out}}
<pre>
$ hopper myname.bas
My Program name: myname.bas
$
</pre>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="applesoftbasic">
<lang ApplesoftBASIC>
10 GOSUB 40"GET PROGRAM NAME
20 PRINT N$
Line 96 ⟶ 184:
670 N$ = "
680 RETURN
</syntaxhighlight>
</lang>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 155 ⟶ 244:
bx lr /* retour procedure */
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
<lang AutoHotkey>
MsgBox, % A_ScriptName
</syntaxhighlight>
</lang>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: TAWK -f PROGRAM_NAME.AWK
#
Line 180 ⟶ 271:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 199 ⟶ 290:
 
Unlike most MS BASICs, [[FreeBASIC]] provides a parsed version of <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgCommand COMMAND$]</code> (called as <code>COMMAND$(n)</code>). <code>COMMAND$(0)</code> is the program's name:
<langsyntaxhighlight lang="qbasic">appname = COMMAND$(0)</langsyntaxhighlight>
 
Additionally, FreeBASIC also provides an analog of [[#C|C's]] <code>argc/argv[]</code>, called <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDdfbargc __FB_ARGC__]</code> and <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDdfbargv __FB_ARGV__]</code>. __FB_ARGV__ can be used to get the program's name like this:
<langsyntaxhighlight lang="qbasic">appname = *__FB_ARGV__(0)</langsyntaxhighlight>
 
See also: [[#PowerBASIC|PowerBASIC]], [[#PureBasic|PureBasic]], [[#Visual Basic|Visual Basic]].
Line 208 ⟶ 299:
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> SYS "GetCommandLine" TO cl%
PRINT $$cl%</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Name1.bas"(A,B,C,S$)
110 CHAIN "Name2.BAS"(A,B,C,S$)
 
Line 220 ⟶ 311:
 
edit 0
start(1,2,3,"Hello")</langsyntaxhighlight>
 
==={{header|BaCon}}===
To get the name with which the program was invoked:
<syntaxhighlight lang="bacon">PRINT TOKEN$(ARGUMENT$, 1)</syntaxhighlight>
To get the full path:
<syntaxhighlight lang="bacon">PRINT ME$</syntaxhighlight>
==={{header|uBasic/4tH}}===
'''CMD(1)''' returns a string with the filename - '''SHOW()''' prints this string.
<syntaxhighlight lang="text">Print Show(Cmd(1))</syntaxhighlight>
 
=={{header|Blue}}==
 
Linux/x86-64. Will print the name of the executable from "argv[0]" as provided.
 
<syntaxhighlight lang="blue">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 ;</syntaxhighlight>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">•name</syntaxhighlight>
 
=={{header|C}}==
Line 227 ⟶ 360:
* <code>argv[0]</code> might be the name of an executable in the PATH, or it might be an absolute or relative path to the executable. At least with [[Unix]], the parent process can set <code>argv[0]</code> to any string, so <code>argv[0]</code> might not be the real name. It is best to pretend that <code>argv[0]</code> has the correct value, but mind that <code>argv[0]</code> might not be an actual file.
 
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int main(int argc, char **argv) {
Line 233 ⟶ 366:
 
return 0;
}</langsyntaxhighlight>
 
To get the source information about some part of code, use compiler defined macros. Most compilers support them or some variation of.
<langsyntaxhighlight lang="c">#include <stdio.h>
int main()
Line 243 ⟶ 376:
__FILE__, __FUNCTION__, __LINE__);
return 0;
}</langsyntaxhighlight>
 
=== BSD ===
Line 256 ⟶ 389:
{{works with|OpenBSD|5.0}} '''To compile myname.c:''' <code>make myname LDLIBS=-lkvm</code>
 
<langsyntaxhighlight lang="c">/* myname.c */
 
#include <sys/param.h>
Line 292 ⟶ 425:
kvm_close(kd);
return 0;
}</langsyntaxhighlight>
 
The program can have three different names!
Line 305 ⟶ 438:
 
{{libheader|Win32}}
<langsyntaxhighlight lang="c">#include <windows.h>
#include <stdlib.h>
#include <wchar.h>
Line 361 ⟶ 494:
free(path);
return 0;
}</langsyntaxhighlight>
 
<pre>Path to executable: C:\Users\kernigh\Documents\field\scratch.exe</pre>
 
=={{header|C++}}==
C++ has difficulty accessing source code filenames, because C code must be compiled into an executable. However, C++ can access the executable's filename.
 
<lang cpp>#include <iostream>
 
using namespace std;
 
int main(int argc, char **argv) {
char *program = argv[0];
cout << "Program: " << program << endl;
 
return 0;
}</lang>
=={{header|C sharp|C#}}==
This effectively outputs the executable name, file path, and any arguments for the current program.
<langsyntaxhighlight lang="csharp">using System;
namespace ProgramName
{
Line 390 ⟶ 510:
}
}
}</langsyntaxhighlight>
In a C# application with a reference to System.Windows.Forms, the following can be used to retrieve the executable name and arguments without the full path.
<langsyntaxhighlight lang="csharp">using System;
namespace ProgramName
{
Line 410 ⟶ 530:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
C++ can access the executable's filename through the arguments to main().
 
<syntaxhighlight lang="cpp">#include <iostream>
 
using namespace std;
 
int main(int argc, char **argv) {
char *program = argv[0];
cout << "Program: " << program << endl;
}</syntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">":";exec lein exec $0 ${1+"$@"}
":";exit
 
Line 424 ⟶ 556:
 
(when (.contains (first *command-line-args*) *source-path*)
(apply -main (rest *command-line-args*)))</langsyntaxhighlight>
 
=={{header|COBOL}}==
{{works with|GnuCOBOL}}
COBOL has an internal PROGRAM-ID name, and then the external invocation name.
 
<syntaxhighlight lang="cobol"> identification division.
program-id. sample.
 
data division.
working-storage section.
01 progname pic x(16).
 
procedure division.
sample-main.
 
display 0 upon argument-number
accept progname from argument-value
display "argument-value zero :" progname ":"
 
display "function module-id :" function module-id ":"
 
goback.
end program sample.</syntaxhighlight>
 
{{out}}
<pre>prompt$ cobc -xj progname.cob
argument-value zero :./progname :
function module-id :sample:</pre>
 
=={{header|CoffeeScript}}==
scriptname.coffee:
<langsyntaxhighlight lang="coffeescript">#!/usr/bin/env coffee
 
main = () ->
Line 434 ⟶ 594:
console.log "Program: " + program
 
if not module.parent then main()</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Shebangs require a special tweak to ~/.clisprc.lisp.
 
<langsyntaxhighlight lang="lisp">;;; Play nice with shebangs
(set-dispatch-macro-character #\# #\!
(lambda (stream character n)
(declare (ignore character n))
(read-line stream nil nil t)
nil))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="lisp">#!/bin/sh
#|
exec clisp -q -q $0 $0 ${1+"$@"}
Line 475 ⟶ 635:
args
:test #'(lambda (x y) (search x y :test #'equalp)))
(main args)))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">#!/usr/bin/env rdmd
 
import std.stdio;
Line 484 ⟶ 644:
void main(in string[] args) {
writeln("Program: ", args[0]);
}</langsyntaxhighlight>
{{out}}
<pre>C:\rosetta>program_name.exe
Program: program_name.exe</pre>
 
<langsyntaxhighlight lang="sh">$ dmd scriptname.d
$ ./scriptname
Program: ./scriptname</langsyntaxhighlight>
 
If the system is configured, the D programming language offers an 'interpreted-looking' mode, which exhibits slightly different behavior than the normal compilation:
<langsyntaxhighlight lang="sh">$ ./scriptname.d
Program: /tmp/.rdmd/Users/andrew/Desktop/src/scriptname/scriptname.d.D3B32385A31B968A3CF8CAF1E1426E5F</langsyntaxhighlight>
 
 
Alternative method using built-in function [http://dlang.org/changelog.html#current_path thisExePath()]
{{works with|D|2.064+}}
<langsyntaxhighlight lang="d">// thisExePath function was introduced in D 2.064 (November 5, 2013)
 
import std.file;
Line 508 ⟶ 668:
{
writeln("Program: ", thisExePath());
}</langsyntaxhighlight>
{{out}} <pre>Z:\rosettacode>program_name.exe
Program: Z:\rosettacode\program_name.exe</pre>
 
=={{header|Dart}}==
<langsyntaxhighlight lang="dart">#!/usr/bin/env dart
 
main() {
var program = new Options().script;
print("Program: ${program}");
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program ProgramName;
 
{$APPTYPE CONSOLE}
Line 528 ⟶ 688:
Writeln('Program name: ' + ParamStr(0));
Writeln('Command line: ' + CmdLine);
end.</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
 
<langsyntaxhighlight lang="dejavu">!print( "Name of this file: " get-from !args 0 )</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="lisp">
(js-eval "window.location.href")
→ "http://www.echolalie.org/echolisp/"
</syntaxhighlight>
</lang>
=={{header|Elena}}==
ELENA 3.4 :
<lang elena>import extensions.
 
=={{header|Ecstasy}}==
public program
<syntaxhighlight lang="ecstasy">
[
module WhatIsMyName {
console writeLine:system'commandLine. // the whole command line
@Inject Console console;
 
void run() {
console.print($"program name: {this:module}");
}
}
</syntaxhighlight>
 
{{out}}
<pre>
program name: WhatIsMyName
</pre>
 
=={{header|Elena}}==
ELENA 4.x :
<syntaxhighlight lang="elena">import extensions;
public program()
{
console.printLine(program_arguments.asEnumerable()); // the whole command line
console writeLine.printLine(program_arguments[0]).; // the program name
}</syntaxhighlight>
]</lang>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">:;exec emacs -batch -l $0 -f main $*
 
;;; Shebang from John Swaby
Line 557 ⟶ 734:
 
(defun main ()
(let ((program (nth 2 command-line-args)))
(message "Program: %s" program)))</langsyntaxhighlight>
 
<code>load-file-name</code> is the ".el" or ".elc" currently being loaded. Within a batch <code>-l</code> it will be the script name, but within sub-loads like <code>require</code> etc it is that sub-load.
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
writeLine("path: " + Runtime.path)
writeLine("name: " + Runtime.name)
writeLine("args: " + Runtime.args)
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\ProgramName.emal sample arguments
path: C:\tmp\Org\RosettaCode\ProgramName.emal
name: ProgramName.emal
args: [sample,arguments]
</pre>
 
=={{header|Erlang}}==
Line 566 ⟶ 757:
When compiled Erlang's macros hold information about the running module.
 
<langsyntaxhighlight lang="erlang">%% Compile
%%
%% erlc scriptname.erl
Line 580 ⟶ 771:
Program = ?FILE,
io:format("Program: ~s~n", [Program]),
init:stop().</langsyntaxhighlight>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">constant cmd = command_line()
puts(1,cmd[2])</langsyntaxhighlight>
 
=={{header|F Sharp|F#}}==
Line 594 ⟶ 785:
* Run as a dotslashed program in Unix (./scriptname.fsx)
 
<langsyntaxhighlight lang="fsharp">#light (*
exec fsharpi --exec $0 --quiet
*)
Line 613 ⟶ 804:
 
let main =
printfn "%s" scriptname</langsyntaxhighlight>
 
=={{header|Factor}}==
 
<langsyntaxhighlight lang="factor">#! /usr/bin/env factor
USING: namespaces io command-line ;
Line 624 ⟶ 815:
: main ( -- ) script get print ;
MAIN: main</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth">0 arg type cr \ gforth or gforth-fast, for example
1 arg type cr \ name of script</langsyntaxhighlight>
 
=={{header|Fortran}}==
Please find example runs in the comments at the beginning of the FORTRAN2003 source. Program name verification can deter system attackers. Therefore, the code is provided as a separate easily reused module.
<syntaxhighlight lang="fortran">
<lang FORTRAN>
! program run with invalid name path/f
!
Line 681 ⟶ 872:
write(6,*)'program continues...'
end program name
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Print "The program was invoked like this => "; Command(0) + " " + Command(-1)
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
 
=={{header|FutureBasic}}==
There are several ways to retrieve the program name. These functions return a string containing the name that can be used in various ways.
<syntaxhighlight lang="futurebasic">
print fn ProcessInfoProcessName
print fn RunningApplicationLocalizedName( fn RunningApplicationCurrentApplication )
</syntaxhighlight>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=fc8af45b13a9bb52b6955bab487fc7ac Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sTemp As String
 
Line 701 ⟶ 900:
Next
 
End</langsyntaxhighlight>
Output:
<pre>
Command to start the program was ./CLIOnly.gambas Hello World!
</pre>
 
=={{header|Genie}}==
<syntaxhighlight lang="genie">[indent=4]
init
print args[0]
print Path.get_basename(args[0])
print Environment.get_application_name()
print Environment.get_prgname()</syntaxhighlight>
 
{{out}}
<pre>prompt$ valac programname.gs
prompt$ ./programname
./programname
programname
(null)
(null)</pre>
 
=={{header|Go}}==
scriptname.go:
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 719 ⟶ 934:
func main() {
fmt.Println("Program:", os.Args[0])
}</langsyntaxhighlight>
{{out|Command line session}}
<pre>
Line 735 ⟶ 950:
If you want the script filename, use:
 
<langsyntaxhighlight lang="groovy">#!/usr/bin/env groovy
 
def program = getClass().protectionDomain.codeSource.location.path
 
println "Program: " + program</langsyntaxhighlight>
 
But if you just want the class name, there are easier ways.
 
So, just typing in and running the following in the GroovyConsole environment:
<syntaxhighlight lang ="groovy">println this.class.getName()</langsyntaxhighlight>
yields the following on the first run:
<pre>ConsoleScript0</pre>
Line 758 ⟶ 973:
Haskell has an impure function for this.
 
<langsyntaxhighlight lang="haskell">import System (getProgName)
 
main :: IO ()
main = getProgName >>= putStrLn . ("Program: " ++)</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
write(&progname) # obtain and write out the program name from the keyword &progname
end</langsyntaxhighlight>
 
=={{header|Io}}==
<langsyntaxhighlight lang="io">#!/usr/bin/env io
 
main := method(
Line 777 ⟶ 992:
)
 
if (System args size > 0 and System args at(0) containsSeq("scriptname"), main)</langsyntaxhighlight>
 
=={{header|J}}==
 
<langsyntaxhighlight lang="j">#!/usr/bin/env jconsole
 
program =: monad : 0
Line 793 ⟶ 1,008:
echo 'Program: ', program 0
 
exit ''</langsyntaxhighlight>
 
=={{header|Jakt}}==
The name of the program is stored in the first index of the array passed to main. This is the name of the compiled executable, so when using jakt -cr the value will be a path starting with build/.
 
<syntaxhighlight lang="jakt">
fn main(arguments: [String]) {
let program_name = arguments[0]
println("{}", program_name)
}
</syntaxhighlight>
 
=={{header|Java}}==
Line 802 ⟶ 1,027:
 
You can get the listing of the arguments to the <code>java</code> command through a system property. The first one is the name of the main class that was run. This depends on a system property named "sun.java.command", which might not exist on all Java virtual machines.
<langsyntaxhighlight lang="java">public class ScriptName {
public static void main(String[] args) {
String program = System.getProperty("sun.java.command").split(" ")[0];
System.out.println("Program: " + program);
}
}</langsyntaxhighlight>
 
An alternate solution is to create a dummy inner class, and then retrieve its enclosing class (which is the class the main method is in) through reflection (though this will not work if the code is placed in a method in another source file--it will give the name of the class it is in inside that source file):
<langsyntaxhighlight lang="java">public class ScriptName {
public static void main(String[] args) {
Class c = new Object(){}.getClass().getEnclosingClass();
System.out.println("Program: " + c.getName());
}
}</langsyntaxhighlight>
 
A solution using the security manager:
<langsyntaxhighlight lang="java">public class ScriptName {
public static void main(String[] args) {
Class c = System.getSecurityManager().getClassContext()[0];
System.out.println("Program: " + c.getName());
}
}</langsyntaxhighlight>
 
A solution using the stack trace (requires Java 1.5+):
<langsyntaxhighlight lang="java">public class ScriptName {
public static void main(String[] args) {
String program = Thread.currentThread().getStackTrace()[1].getClassName();
System.out.println("Program: " + program);
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
{{incorrect|JavaScript|Program name, not function name. Show sample output.}}
 
<small><i>Suggestion: document.location.href is probably closer to task requirements (and better than a script name), plus arguments.callee.name is long dead. --[[User:Petelomax|Pete Lomax]] ([[User talk:Petelomax|talk]]) 23:29, 24 March 2022 (UTC)</i></small><br>
There is no capability within the ECMA-262 standard (the standard for ECMAScript, the language underlying JavaScript) for a function to determine its name. Since objects in JavaScript are first class objects, variables and properties are only references to objects. The name of an object might be said to be the name used to reference it, however a single object may have many variables and properties that reference it, or none.
 
In some implementations, the following (non–standard) code will work:
 
<langsyntaxhighlight lang="javascript">function foo() {
return arguments.callee.name;
}</langsyntaxhighlight>
 
But it will fail in in others. JavaScript also has anonymous functions that don't have a name, e.g.:
<langsyntaxhighlight lang="javascript">(function(){alert(arguments.callee.name);}())</langsyntaxhighlight>
returns an empty string or <code>undefined</code> even where the first example works.
 
Line 850 ⟶ 1,076:
Node.js has a global variable for this.
 
<langsyntaxhighlight lang="javascript">#!/usr/bin/env node
/*jslint nodejs:true */
 
Line 858 ⟶ 1,084:
}
 
if (!module.parent) { main(); }</langsyntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">#!/usr/bin/joy
 
argv first putchars.</syntaxhighlight>
 
=={{header|Jsish}}==
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* Program name, in Jsish */
puts('Executable:', Info.executable());
puts('Argv0 :', Info.argv0());</syntaxhighlight>
 
{{out}}
<pre>prompt$ jsish programName.jsi
Executable: /usr/local/bin/jsish
Argv0 : /home/btiffin/forge/jsi/jsi-test/rosetta/programName.jsi</pre>
 
=={{header|Julia}}==
Julia strips the program file name from <tt>ARGS</tt>, so this information is not available to the program from its command line arguments. Instead it is accessible via <tt>Base.source_path()</tt>.
<syntaxhighlight lang="julia">
<lang Julia>
prog = basename(Base.source_path())
println("This program file is \"", prog, "\".")
</syntaxhighlight>
</lang>
 
{{out}}
Line 873 ⟶ 1,115:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
// 'progname.kt' packaged as 'progname.jar'
Line 880 ⟶ 1,122:
println(System.getProperty("sun.java.command")) // may not exist on all JVMs
println(System.getProperty("java.vm.name"))
}</langsyntaxhighlight>
 
{{out}}
Line 887 ⟶ 1,129:
Java HotSpot(TM) 64-Bit Server VM
</pre>
 
=={{header|langur}}==
The script name is in the _script variable (separate from the arguments, in the _args variable).
<syntaxhighlight lang="langur">writeln "script: ", _script
writeln "script args: ", _args</syntaxhighlight>
 
{{out}}
<pre>script: ./testargs.langur
script args: []</pre>
 
=={{header|Lasso}}==
<langsyntaxhighlight lang="lasso">#!/usr/bin/lasso9
 
stdoutnl("Program: " + $argv->first)</langsyntaxhighlight>
Output:
<langsyntaxhighlight lang="shell">$ lasso9 script_name.lasso
Program: script_name.lasso</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
nSize = _MAX_PATH + 2
lpFilename$ = space$(nSize); chr$(0)
Line 940 ⟶ 1,191:
end function
 
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="text">
Path to LB exe
C:\progs\Liberty BASIC v4.04\liberty.exe
current program file (:last one on LRU list)
C:\progs\Liberty BASIC v4.04\untitled.bas
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">put _player.applicationName
-- "lsw.exe"
put _movie.name
-- "lsw_win_d11.dir"</langsyntaxhighlight>
 
=={{header|LLVM}}==
Like C, LLVM can use argv to access the executable's filename.
 
<langsyntaxhighlight lang="sh">$ make
llvm-as scriptname.ll
llc -disable-cfi scriptname.bc
gcc -o scriptname scriptname.s
./scriptname
Program: ./scriptname</langsyntaxhighlight>
 
Makefile
 
 
<langsyntaxhighlight lang="make">all: scriptname.ll
llvm-as scriptname.ll
llc scriptname.bc
Line 977 ⟶ 1,228:
-rm scriptname
-rm scriptname.s
-rm scriptname.bc</langsyntaxhighlight>
 
<langsyntaxhighlight lang="llvm">@msg_main = internal constant [13 x i8] c"Program: %s\0A\00"
 
declare i32 @printf(i8* noalias nocapture, ...)
Line 988 ⟶ 1,239:
 
ret i32 0
}</langsyntaxhighlight>
 
=={{header|Lua}}==
Lua's arg is like C's argv.
 
<langsyntaxhighlight lang="lua">#!/usr/bin/env lua
 
function main(arg)
Line 1,004 ⟶ 1,255:
else
module(..., package.seeall)
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Declare GetModuleFileName Lib "kernel32.GetModuleFileNameW" {Long hModule, &lpFileName$, Long nSize}
Line 1,017 ⟶ 1,269:
Checkit
\\ command$ return the file's path plus name of script
\\ we3we can use edit "callme.gsb" to paste these, and use USE callme to call it from M2000 console.
Module SayIt {
Show
Line 1,024 ⟶ 1,276:
}
SayIt
</syntaxhighlight>
</lang>
 
=={{header|Make}}==
 
<langsyntaxhighlight lang="make">NAME=$(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
 
all:
@echo $(NAME)
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}==
 
<lang mathematica>#!/usr/bin/env MathKernel -script
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">#!/usr/bin/env MathKernel -script
ScriptName[] = Piecewise[
{
Line 1,044 ⟶ 1,294:
$CommandLine[[Position[$CommandLine, "-script", 1][[1,1]] + 1]]
]
 
Program = ScriptName[];
Print["Program: " <> Program]</syntaxhighlight>
 
Print["Program: " <> Program]</lang>
 
 
=={{header|Mercury}}==
 
<langsyntaxhighlight lang="mercury">:- module program_name.
:- interface.
 
Line 1,067 ⟶ 1,314:
io.print_line(ProgName, !IO).
 
:- end_module program_name.</langsyntaxhighlight>
 
=={{header|Mozart/Oz}}==
 
Makefile:
<langsyntaxhighlight lang="make">all: test
 
test: scriptname
Line 1,082 ⟶ 1,329:
clean:
-rm scriptname
-rm *.exe</langsyntaxhighlight>
 
scriptname.oz:
<langsyntaxhighlight lang="oz">functor
import
System
Line 1,096 ⟶ 1,343:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
<syntaxhighlight lang="nanoquery">println args[1]</syntaxhighlight>
{{out}}
<pre>programname.nq</pre>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System.Environment;
...
def program_name = GetCommandLineArgs()[0];
...</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,113 ⟶ 1,365:
say 'Program:' System.getProperty('sun.java.command')
return
</syntaxhighlight>
</lang>
'''Output'''
 
Line 1,133 ⟶ 1,385:
newLISP has a function, (main-args int) for this.
 
<langsyntaxhighlight lang="lisp">#!/usr/bin/env newlisp
 
(let ((program (main-args 1)))
(println (format "Program: %s" program))
(exit))</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import os
echo getAppFilename() # Prints the full path of the executed file
echo paramStr(0) # Prints argv[0]</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
Works with oo2c Version 2
<langsyntaxhighlight lang="oberon2">
MODULE ProgramName;
IMPORT
Line 1,154 ⟶ 1,406:
Out.Object("Program name: " + Args.Get(0));Out.Ln
END ProgramName.
</syntaxhighlight>
</lang>
Output:
<pre>
Program name: ./ProgramName
</pre>
 
=={{header|Objective-C}}==
 
scriptname.m:
 
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main(int argc, char **argv) {
Line 1,178 ⟶ 1,431:
 
return 0;
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="sh">$ gcc -o scriptname -framework foundation scriptname.m
$ ./scriptname
Program: ./scriptname</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let () =
print_endline Sys.executable_name;
print_endline Sys.argv.(0)</langsyntaxhighlight>
 
<pre>
Line 1,208 ⟶ 1,461:
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">function main()
program = program_name();
printf("Program: %s", program);
endfunction
 
main();</langsyntaxhighlight>
 
=={{header|Ol}}==
First argument of *vm-args* is an executing program name.
<langsyntaxhighlight lang="scheme">
(print (car *vm-args*))
</syntaxhighlight>
</lang>
 
=={{header|Order}}==
This is relatively trivial in Order, as the program being executed is a macro expression in the current C program file being read by the compiler:
<syntaxhighlight lang ="c">__FILE__</langsyntaxhighlight>
When including another file containing another <code>ORDER_PP</code> expression, within that file the <code>__FILE__</code> macro will expand to the name of that file; but arguably that expression constitutes a separate Order program within the greater C project.
 
Line 1,231 ⟶ 1,484:
=={{header|Pascal}}==
 
<langsyntaxhighlight lang="pascal">program ScriptName;
var
prog : String;
Line 1,238 ⟶ 1,491:
write('Program: ');
writeln(prog)
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/env perl
 
use strict;
Line 1,251 ⟶ 1,504:
}
 
unless(caller) { main; }</langsyntaxhighlight>
 
<code>$0</code> includes the full path if a script is run as say <code>perl /some/dir/foo.pl</code>. The <code>FindBin</code> module can give just the basename. This can be good for printing in diagnostics etc.
<langsyntaxhighlight Perllang="perl">use FindBin;
print "Program name $FindBin::Script\n";</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{works with|rakudo|2015-10-16}}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
In Perl 6, the name of the program being executed is in the special global variable <tt>$*PROGRAM-NAME</tt>.
<span style="color: #004080;">string</span> <span style="color: #000000;">cl2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
<lang perl6>say $*PROGRAM-NAME;</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cl2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- full path</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_file_name</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- eg test.exw or test.exe or test.htm</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_file_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- eg test</span>
<!--</syntaxhighlight>-->
 
=={{header|PhixPhixmonti}}==
<syntaxhighlight lang="phixmonti">argument 1 get ?</syntaxhighlight>
<lang Phix>puts(1,command_line()[2])</lang>
 
=={{header|PHP}}==
PHP has a global dictionary for this.
 
<langsyntaxhighlight lang="php"><?php
$program = $_SERVER["SCRIPT_NAME"];
echo "Program: $program\n";
?></langsyntaxhighlight>
 
=={{header|PicoLisp}}==
The function '[http://software-lab.de/doc/refC.html#cmd cmd]' returns the command name.
<langsyntaxhighlight PicoLisplang="picolisp">: (cmd)
-> "/usr/bin/picolisp"</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
Line 1,282 ⟶ 1,539:
Previous versions of PowerBASIC ([[PB/Win]] 8 or older; [[PB/CC]] 4 or older) have to make an [[API]] call:
 
<langsyntaxhighlight lang="powerbasic">#INCLUDE "Win32API.inc"
'[...]
DIM fullpath AS ASCIIZ * 260, appname AS STRING
Line 1,290 ⟶ 1,547:
ELSE
appname = fullpath
END IF</langsyntaxhighlight>
 
{{works with|PowerBASIC for Windows|9}}
Line 1,296 ⟶ 1,553:
 
Recent versions of PowerBASIC provide the <code>EXE</code> object; <code>EXE.NAME$</code> returns the program's name, while <code>EXE.NAMEX$</code> returns the program's name and extension. (<code>EXE.EXTN$</code> returns the extension only.) So, to get the program's name, we do this:
<langsyntaxhighlight lang="powerbasic">appname = EXE.NAMEX$</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
# write this in file <program.ps1>
$MyInvocation.MyCommand.Name
# launch with <.\program>
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,310 ⟶ 1,567:
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">% SWI-Prolog version 8.0.0 for i686-linux.
<lang Prolog>
% This will find itself, and return the knowledge base it is in.
% SWI-Prolog version 7.6.4 for i386.
% This will find itself, and return the file it is in.
file_name(F) :- true
, M = user % M is the module .
, P = file_name(_) % P is the predicate .
, source_file(M:P, F) % F is the filenamefile .
, \+ predicate_property(M:P, imported_from(_))
.</syntaxhighlight>
.
 
</lang>
Alternatively, you may prefer a list of all your knowledge bases; adding the following code to each of your knowledge bases will allow you to query <code>findall(F, source_file(this_is_one_of_my_files, F), L).</code>.
<syntaxhighlight lang="prolog">:- multifile(this_is_one_of_my_files). this_is_one_of_my_files.</syntaxhighlight>
 
=={{header|PureBasic}}==
PureBasic provides a way to retrieve the filename of the program being executed. It includes the file's path.
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()
PrintN(ProgramFilename())
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
Sample output when executing the above program compiled to an executible file called 'program name.exe':
<pre>H:\Data\Rosetta Code\program name.exe</pre>
Line 1,335 ⟶ 1,593:
Python has at least two ways to get the script name: the traditional ARGV and the inspect module.
 
<langsyntaxhighlight lang="python">#!/usr/bin/env python
 
import sys
Line 1,344 ⟶ 1,602:
 
if __name__ == "__main__":
main()</langsyntaxhighlight>
 
 
<langsyntaxhighlight lang="python">#!/usr/bin/env python
 
import inspect
Line 1,356 ⟶ 1,614:
 
if __name__ == "__main__":
main()</langsyntaxhighlight>
 
=={{header|R}}==
R's syntax is complicated, but doable.
 
<langsyntaxhighlight Rlang="r">#!/usr/bin/env Rscript
 
getProgram <- function(args) {
Line 1,372 ⟶ 1,630:
cat("Program: ", program, "\n")
 
q("no")</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#!/usr/bin/env racket
#lang racket
 
(define (program) (find-system-path 'run-file))
 
(module+ main (printf "Program: ~a\n" (program)))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-10-16}}
In Raku, the name of the program being executed is in the special global variable <tt>$*PROGRAM-NAME</tt>.
<syntaxhighlight lang="raku" line>say $*PROGRAM-NAME;</syntaxhighlight>
 
=={{header|Raven}}==
<langsyntaxhighlight Ravenlang="raven">ARGS list " " join "%s\n" print</langsyntaxhighlight>
{{out}}
<pre>raven arg_file.rv</pre>
Line 1,396 ⟶ 1,660:
<br><br>It should be noted that the format of the complete path varies depending upon the operating system.
 
<langsyntaxhighlight REXXlang="rexx">/* Rexx */
 
Parse source . . pgmPath
Say pgmPath
</syntaxhighlight>
</lang>
;Output
<pre>
Line 1,409 ⟶ 1,673:
REXX does not support the use of arg(0) to access the program name. A workaround is to use a shell wrapper script to obtain and provide the invocation name of the wrapper:
 
<langsyntaxhighlight lang="sh">#!/bin/sh
rexxbit.rexx $0 $*</langsyntaxhighlight>
 
Here is a rexx script that makes use of this:
 
<langsyntaxhighlight lang="rexx">say "The program is called " arg(1)</langsyntaxhighlight>
 
On TSO, this program
<langsyntaxhighlight lang="rexx">/*REXX program RANG1 in PDS N561985.PRIV.CLIST W. Pachl */
Parse Source a b c
Say 'a='a
Say 'b='!!b
Say 'c='c </langsyntaxhighlight>
yields
<pre>
Line 1,437 ⟶ 1,701:
:::* ROO REXX
:::* ooRexx
<langsyntaxhighlight lang="rexx">/*REXX pgm displays the name (& possible path) of the REXX program name.*/
parse version _version
parse source _system _howInvoked _path
Line 1,451 ⟶ 1,715:
call prog_nam , 'subroutine' /*call ourself as a subroutine. */
zz = prog_nam( , 'function') /* " " " " function. */
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
'''output''' &nbsp; when using '''BREXX''' with the input of: &nbsp; <tt> command </tt>
<pre>
Line 1,551 ⟶ 1,815:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "Active Source File Name : " + filename() + nl
</syntaxhighlight>
</lang>
output
<langsyntaxhighlight lang="ring">
Active Source File Name : tests\filename.ring
</syntaxhighlight>
</lang>
 
Check the main file in the program
<langsyntaxhighlight lang="ring">
if sysargv[2] = filename()
see "I'm the main program file!" + nl
Line 1,567 ⟶ 1,831:
see "I'm a sub file in a program" + nl
ok
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">#!/usr/bin/env ruby
 
puts "Path: #{$PROGRAM_NAME}" # or puts "Path: #{$0}"
puts "Name: #{File.basename $0}"</langsyntaxhighlight>
 
For example,
Line 1,591 ⟶ 1,855:
scriptname.rs:
 
<langsyntaxhighlight lang="rust">fn main() {
println!("Program: {}", std::env::args().next().unwrap());
}</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight lang="sh">$ rustc scriptname.rs
$ ./scriptname
Program: ./scriptname</langsyntaxhighlight>
 
=={{header|SAC}}==
Line 1,605 ⟶ 1,869:
scriptname.sac:
 
<langsyntaxhighlight lang="c">use StdIO: all;
use Array: all;
use String: { string };
Line 1,614 ⟶ 1,878:
printf("Program: %s\n", program);
return(0);
}</langsyntaxhighlight>
 
Makefile:
 
<langsyntaxhighlight lang="make">all: scriptname
 
scriptname: scriptname.sac
Line 1,625 ⟶ 1,889:
clean:
-rm scriptname
-rm scriptname.c</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight lang="sh">$ make
sac2c -o scriptname scriptname.sac
$ ./scriptname
Program: ./scriptname</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object ScriptName extends App {
println(s"Program of instantiated object: ${this.getClass.getName}")
// Not recommended, due various implementations
println(s"Program via enviroment: ${System.getProperty("sun.java.command")}")
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
Line 1,645 ⟶ 1,909:
Getting the program name is tricky. When interpreted, the script name will be printed. When compiled, the executable name will be printed.
 
<langsyntaxhighlight lang="scheme">#!/bin/sh
#|
exec csi -ss $0 ${1+"$@"}
Line 1,669 ⟶ 1,933:
 
(if (equal? (car (program)) 'compiled)
(main (cdr (argv))))</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 1,676 ⟶ 1,940:
When the program is compiled this is the path of the executable.
The functions ''dir(PROGRAM)'' and ''file(PROGRAM)'' deliver the directory respectivly file name of the program path.
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,685 ⟶ 1,949:
writeln("Program directory: " <& dir(PROGRAM));
writeln("Program file: " <& file(PROGRAM));
end func;</langsyntaxhighlight>
 
Output when the program is interpreted:
Line 1,702 ⟶ 1,966:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say __MAIN__;
if (__MAIN__ != __FILE__) {
say "This file has been included!";
}</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 1,712 ⟶ 1,976:
Note that this only works when run as "./scriptname.st", because the shebang must force the script name onto ARGV.
 
<langsyntaxhighlight lang="smalltalk">"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
 
Line 1,719 ⟶ 1,983:
program := Smalltalk getArgv: 1.
 
Transcript show: 'Program: ', program; cr.</langsyntaxhighlight>
 
{{works with|Smalltalk/X}}
Works when run in script mode or in a workspace.
 
<langsyntaxhighlight lang="smalltalk">| program |
 
program := Smalltalk commandLine first.
Transcript show: 'Program: ', program; cr.</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">#!/usr/bin/env sml
 
let
Line 1,736 ⟶ 2,000:
in
print ("Program: " ^ program ^ "\n")
end;</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">#!/usr/bin/env tclsh
 
proc main {args} {
Line 1,748 ⟶ 2,012:
if {$::argv0 eq [info script]} {
main {*}$::argv
}</langsyntaxhighlight>
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
MainModule: {
_start: (λ
(textout (get @progArgs 0))
)
}</syntaxhighlight>
 
=={{header|TXR}}==
Line 1,754 ⟶ 2,027:
Given this code in <code>program-name.txr</code>, marked executable:
 
<langsyntaxhighlight lang="txr">#!/usr/local/bin/txr -B
@(bind my-name @self-path)</langsyntaxhighlight>
 
If we run it as an executable:
Line 1,780 ⟶ 2,053:
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">#!/bin/sh
 
echo "Program: $0"</langsyntaxhighlight>
 
=={{header|Vala}}==
Get name of program and print to console:
<langsyntaxhighlight lang="vala">
public static void main(string[] args){
string command_name = args[0];
Line 1,792 ⟶ 2,065:
stdout.printf("%s\n", command_name);
}
</syntaxhighlight>
</lang>
 
Output for program named "filename":
Line 1,802 ⟶ 2,075:
 
VBA can retrieve the name of the program hosting the VBA code using the <code>Application</code> object:
<syntaxhighlight lang ="vb">Debug.Print Application.Name</langsyntaxhighlight>
 
This is mostly useful for code that is shared between, say, [[wp:Microsoft Excel|Microsoft Excel]] and [[wp:Microsoft Word|Microsoft Word]], but has different requirements or actions depending on where it's running.
 
Wscript.Echo "FullName:",Wscript.FullName
 
=={{header|vbscript}}==
 
vbscript provides the Wscript object. Among its properties are the following:
 
<langsyntaxhighlight lang="vbscript">
Wscript.Echo "FullName:",Wscript.FullName
Wscript.Echo "Name:",Wscript.Name
Line 1,817 ⟶ 2,091:
Wscript.Echo "ScriptFullName:",Wscript.ScriptFullName
Wscript.Echo "ScriptName:",Wscript.ScriptName
</syntaxhighlight>
</lang>
 
<pre>
Line 1,830 ⟶ 2,104:
 
Visual Basic provides the <code>App</code> object, which has a property called <code>EXEName</code> that contains the program's filename ''without the extension''. (For most uses, this doesn't matter, but for code shared between, for example, a program and a screensaver, it can be important.) So, if a program is called "MyVBapp.exe", retreiving the value of <code>App.EXEName</code> would look like this:
<langsyntaxhighlight lang="vb">appname = App.EXEName 'appname = "MyVBapp"</langsyntaxhighlight>
 
Alternately, Visual Basic can make an [[API]] call:
<langsyntaxhighlight lang="vb">Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Dim fullpath As String * 260, appname As String, namelen As Long
namelen = GetModuleFileName (0, fullpath, 260)
Line 1,841 ⟶ 2,115:
Else
appname = fullpath
End If</langsyntaxhighlight>
 
=={{header|Wren}}==
Assuming we're running a script file named "Program_name.wren":
<syntaxhighlight lang="wren">import "os" for Process
 
System.print("My name is %(Process.allArguments[1])")</syntaxhighlight>
 
{{out}}
<pre>
My name is Program_name.wren
</pre>
 
=={{header|x86 Assembly}}==
Line 1,849 ⟶ 2,132:
===Linux===
Makefile:
<langsyntaxhighlight lang="make">FORMAT=-f elf
RUN=./
BIN=scriptname
Line 1,867 ⟶ 2,150:
clean:
-rm $(BIN)
-rm $(OBJ)</langsyntaxhighlight>
 
scriptname.asm:
<langsyntaxhighlight lang="asm">bits 32
 
section .data
Line 1,941 ⟶ 2,224:
mov eax, sys_exit
mov ebx, 0
int kernel</langsyntaxhighlight>
 
===FreeBSD/Mac OS X===
Makefile:
<langsyntaxhighlight lang="make"># FreeBSD defaults
 
FORMAT=-f elf
Line 1,971 ⟶ 2,254:
clean:
-rm $(BIN)
-rm $(OBJ)</langsyntaxhighlight>
 
scriptname.asm:
<langsyntaxhighlight lang="asm">bits 32
 
section .data
Line 2,052 ⟶ 2,335:
mov eax, sys_exit
sub esp, 4
int kernel</langsyntaxhighlight>
 
===Windows===
Makefile:
<langsyntaxhighlight lang="make">FORMAT=-f win32
BIN=scriptname.exe
OBJ=scriptname.obj
Line 2,074 ⟶ 2,357:
clean:
-rm $(BIN)
-rm $(OBJ)</langsyntaxhighlight>
 
scriptname.asm:
<langsyntaxhighlight lang="asm">bits 32
 
section .data
Line 2,170 ⟶ 2,453:
 
push 0
call ExitProcess</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">print peek$("program_name")
 
s$ = system$("cd")
n = len(s$)
print left$(s$, n - 2), "\\", peek$("program_name")</langsyntaxhighlight>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
 
const debug = std.debug;
const heap = std.heap;
const process = std.process;
 
pub fn main() !void {
var args = process.args();
 
const program_name = try args.next(heap.page_allocator) orelse unreachable;
defer heap.page_allocator.free(program_name);
 
debug.warn("{}\n", .{program_name});
}</syntaxhighlight>
 
=={{header|zkl}}==
C's argv is exposed to the zkl runtime so if file bbb.zkl contains:
<langsyntaxhighlight lang="zkl">#!/Homer/craigd/Bin/zkl
println(System.argv);</langsyntaxhighlight>
Then (on Unix like OSes)
<langsyntaxhighlight lang="zkl">./bbb.zkl
zkl bbb.zkl</langsyntaxhighlight>
both print
<pre>
Line 2,191 ⟶ 2,490:
</pre>
On Unix, zkl is actually a shell script:
<langsyntaxhighlight lang="bash">#!/bin/sh
# A bash script to run zkl if you haven't jumped
# through all the Unix hoops to put the bits in the "right" places
Line 2,205 ⟶ 2,504:
export zklRoot
#set -o noglob
LD_LIBRARY_PATH=$zklRoot/Lib $zklRoot/Bin/zkl "$@"</langsyntaxhighlight>
On Windows, no launch script (windows knows where the DLLs are) but argv[0] can be messed up.
 
{{omit from|EasyLang}}
 
{{omit from|GUISS}}
{{omit from|Retro}}
162

edits