Skip to main content
Not restricted to Linux
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71

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 running on GNU/Linuxand the ps command 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 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

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 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

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
it doesn't invoke any more "subshells" than the other command.
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

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 heresame approach, but you need to invoke a sub-shell to retrieve itderive 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

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 { 
  ("ps -p $PPID -o comm=") | getline CMDNAME
  print CMDNAME
}

###Testing

Run the script like this:

./cmdname.awk

Output in both cases:

cmdname.awk

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 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
Added defensive parenthesis
Source Link
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 { 
  ("ps -p $PPID -o comm=") | getline CMDNAME
  print CMDNAME
}

###Testing

Run the script like this:

./cmdname.awk

Output in both cases:

cmdname.awk

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 { 
  "ps -p $PPID -o comm=" | getline CMDNAME
  print CMDNAME
}

###Testing

Run the script like this:

./cmdname.awk

Output in both cases:

cmdname.awk

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 { 
  ("ps -p $PPID -o comm=") | getline CMDNAME
  print CMDNAME
}

###Testing

Run the script like this:

./cmdname.awk

Output in both cases:

cmdname.awk
deleted 2 characters in body
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71
Loading
Avoid the redundant sub-shell as suggested by Stephane Chazelas
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71
Loading
Clearer about output
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71
Loading
edited body
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71
Loading
Added mawk and nawk alternatives
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71
Loading
Ensure interpreter is GNU awk
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71
Loading
Source Link
Thor
  • 17.5k
  • 3
  • 55
  • 71
Loading