Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

In bash $0 contains the name of the script, but in awk if I make a script named myscript.awk with the following content:

#!/usr/bin/awk -f
BEGIN{ print ARGV[0] }

and run it, it will only print "awk". Besides, ARGV[i] with i>0 is used only for script arguments in command line. So, how to make it print the name of the script, in this case "myscript.awk"?

share|improve this question
    
I've changed the title from awk to mawk because all the solutions require gawk and don't work with general awk, and in particular with mawk which is widely used (e.g. default on Ubuntu) –  cipper 5 hours ago
    
What makes you think mawk is default on Ubuntu? On my 15.04 VM, the default awk is gawk. While mawk is installed it is not the default. –  terdon 25 mins ago

4 Answers 4

With POSIX awk:

#!/usr/bin/awk -f

BEGIN {
    print ENVIRON["AWKSCRIPT"]
}

Then:

AWKSCRIPT=test.awk ./test.awk
test.awk
share|improve this answer
3  
You manually feed the name of the script in it, this is not a self-printing way –  cipper 19 hours ago
    
@cipper: Well, that's the easiest and portable way I can imagine. –  cuonglm 19 hours ago
    
It's like calling awk -vNAME="myscript.awk" ./myscript.awk and then print the variable NAME inside the script. Not a solution. –  cipper 5 hours ago
    
@cipper: That's the only way, if you mention mawk. And also using ENVIRON isn't the same as using -vNAME="myscript.awk", since when mawk will expand escape sequence in NAME. –  cuonglm 5 hours ago

I don't think this is possible as per gawk documentation:

Finally, the value of ARGV[0] (see section 7.5 Built-in Variables) varies depending upon your operating system. Some systems put awk there, some put the full pathname of awk (such as /bin/awk), and some put the name of your script ('advice'). Don't rely on the value of ARGV[0] to provide your script name.

On linux you can try using a kind of a dirty hack:

#!/usr/bin/awk -f

BEGIN { getline t < "/proc/self/cmdline"; split(t, a, "\0"); print a[3]; }
share|improve this answer
    
your script as is seems not working. It just prints "k" if called with "awk -f script.awk", and it prints "s" if called by "./script.awk" –  cipper 19 hours ago
    
@cipper: Here it works with gawk and fails (like your description) with mawk. Interesting! –  yeti 19 hours ago
    
It works for me in linux, awk - 4.0.2. In freebsd with /proc/curpoc/cmdline, and awk result is like yours but works with gawk. –  taliezin 19 hours ago
    
On default ubuntu it does not work. It would be nice to find a portable solution. –  cipper 19 hours ago
    
@cipper, consider cuonglm answer. –  taliezin 19 hours ago

I don't know any direct way of getting the command name from within awk. With GNU awk running on GNU/Linux you can use the process ID 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
}

Output:

cmdname.awk
share|improve this answer
    
I got an error: /bin/sh: 1: -o: not found –  cipper 19 hours ago
    
@cipper: This only works with GNU awk, I added the missing shebang line. –  Thor 19 hours ago
    
From gawk manual: According to POSIX, ‘expression | getline’ is ambiguous if expression contains unparenthesized operators other than ‘$’—for example, ‘"echo " "date" | getline’ is ambiguous because the concatenation operator is not parenthesized. You should write it as ‘("echo " "date") | getline’ if you want your program to be portable to all awk implementations. –  cipper 19 hours ago
    
but we cannot use parentheses in your method because PROCINFO would be interpreted as bash command as well... –  cipper 19 hours ago
1  
If it needs gawk it is a gawk solution instead of an awk solution. I think @cipper should add his wish "a portable solution" to the question. –  yeti 18 hours ago

Using GNU awk

Checking the GNU awk user's guide - 7.5.2 Built-in Variables That Convey Information I stumbled upon:

PROCINFO #

The elements of this array provide access to information about the running awk program. The following elements (listed alphabetically) are guaranteed to be available:

PROCINFO["pid"]

The process ID of the current process.

This means that you can know the PID of the program during runtime. Then, it is a matter of using system() to look for the process with this given PID:

#!/usr/bin/gawk -f
BEGIN{ pid=PROCINFO["pid"]
       system("ps -ef | awk '$2==" pid " {print $NF}'")
}

I use ps -ef, which displays the PID on the 2nd column. Assuming the executiong is done through awk -f <script> and no other parameters, we can assume the last field of the line contains the information we want.

In case we had some parameters, we would have to parse the line differently -or, better, use some of the options of ps to print just the columns we are interested in.

Test

$ awk -f a.awk 
a.awk
$ cp a.awk hello.awk
$ awk -f hello.awk 
hello.awk

Note also that another chapter of the GNU awk user's guide tells us that ARGV is not the way to go:

1.1.4 Executable awk Programs

Finally, the value of ARGV[0] (see Built-in Variables) varies depending upon your operating system. Some systems put ‘awk’ there, some put the full pathname of awk (such as /bin/awk), and some put the name of your script (‘advice’). (d.c.) Don’t rely on the value of ARGV[0] to provide your script name.

share|improve this answer
    
unfortunately PROCINFO is only a gawk feature, not general awk. For example it is not available in mawk (which is installed by default in ubuntu) –  cipper 5 hours ago
    
I know... Why did you tag the question with [gawk] then? –  fedorqui 5 hours ago
    
You're right. When I posted the question I wasn't aware about all these differences between mawk and gawk. The tag has changed to mawk now. –  cipper 4 hours ago
    
@cipper good : ) I was in fact testing with mawk and couldn't make it work, so that I installed gawk in my Ubuntu and it worked. So a workaround can be to use gawk : D –  fedorqui 3 hours ago
1  
@cipper gawk is also installed by default on all Debian-based distros. I am pretty sure it is installed by default on 100% of all GNU/Linux systems in general. It's a basic GNU tool after all. You will find embedded systems and unices with only mawk or pure awk but I very much doubt you will ever find a Linux distribution with no gawk. –  terdon 23 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.