[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is the name of the file being interpreted available?

From: Andrew J. Schorr
Subject: Re: is the name of the file being interpreted available?
Date: Sun, 19 Jun 2022 12:09:23 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Sun, Jun 19, 2022 at 10:28:32AM -0500, Ed Morton wrote:
> I just realised that while shell has $0 to hold the name of the file
> containing the script being interpreted, I don't know if that same
> information is available in any builtin variable in awk. Obviously I
> could write:
>     awk -v script='/foo/bar/script.awk' -f '/foo/bar/script.awk' input
> or similar but is there any way to get that '/foo/bar/script.awk'
> info inside the awk script without any manual intervention so I can
> just write:
>     BEGIN { "we are running", foo }
> for some value of `foo` inside the script to output:
>     we are running /foo/bar/script.awk

I don't think that's currently exposed, although you can see the full
commandline in the PROCINFO[argv] array.

'The PROCINFO["argv"] array contains all of the command-line arguments (after
glob expansion and redirection processing on platforms where that must be done
manually by the program) with subscripts ranging from 0 through argc - 1. For
example, PROCINFO["argv"][0] will contain the name by which gawk was invoked. '

You can test for yourself by dumping out the contents of the SYMTAB array to
see everything that is defined.  For example:

bash-4.2$ cat /tmp/test.gawk 
function printvar(what, val,   i) {
   if (isarray(val)) {
      for (i in val)
         printvar((what "[" i "]"), val[i])
      printf "%s = [%s]\n", what, val

   for (i in SYMTAB) {
      printvar(i, SYMTAB[i])

bash-4.2$ gawk -f /tmp/test.gawk | grep test.gawk
PROCINFO[argv][2] = [/tmp/test.gawk]

There are some subtleties here. When you say "the script being interpreted",
how would you handle situations where awk source file A contains function X
that calls a function Y located in source file B? If one is executing function
Y called from X, is the current source file B or A? And so on.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]