Skip to main content
5 of 10
Clearer about output
Thor
  • 17.5k
  • 3
  • 55
  • 71

I don't know any direct way of getting the command name from within awk.

###gawk

With GNU awk running on GNU/Linux you can use the process ID from PROCINFO["PID"] and ps 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 still use the PID here, but you need to invoke a sub-shell to retrieve it:

cmdname.awk

#!/usr/bin/mawk -f

BEGIN { 
  "echo $PPID" | getline PID
  ("ps -p " PID " -o comm=") | getline CMDNAME
  print CMDNAME
}

###Testing

Run the script like this:

./cmdname.awk

Output in both cases:

cmdname.awk
Thor
  • 17.5k
  • 3
  • 55
  • 71