I don't know any direct way of getting the command name from within awk. You can however find it through a sub-shell.
gawk
With GNU awk and the ps
command you can use the process ID from PROCINFO["PID"]
to retrieve the command name as a workaround. For example:
cmdname.awk
#!/usr/bin/gawk -f
BEGIN {
("ps -p " PROCINFO["pid"] " -o comm=") | getline CMDNAME
print CMDNAME
}
mawk and nawk
You can use the same approach, but derive awk
's PID from the $PPID
special shell variable (PID of the parent):
cmdname.awk
#!/usr/bin/mawk -f
BEGIN {
("ps -p $PPID -o comm=") | getline CMDNAME
print CMDNAME
}
Testing
Run the script like this:
./cmdname.awk
Output in both cases:
cmdname.awk