Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Awk, are $_ and $0 synonyms?

Tags:

awk

I find no reference to $_ in the Gnu Awk manual or the Wikipedia page, but I used it by mistake from a Perl habit and it seems to be identical to $0.

ping www.bbc.co.uk | awk '{print NR,$_}'
ping www.bbc.co.uk | awk '{print NR,$0}'

Is it some very old deprecated behaviour nobody's bothered to remove?

like image 690
Tim Baverstock Avatar asked Sep 07 '25 23:09

Tim Baverstock


1 Answers

In OP's code:

ping www.bbc.co.uk | awk '{print NR,$_}'

Explanation: Above awk code its considering _ as a variable here, since no value is defined for variable _ so its value is zero(0) that's why it becomes from NR,$_ to NR,$0 and hence printing current line. So there is no special meaning for $_ unlike perl etc.

So even someone tries like: ping www.bbc.co.uk | awk '{print NR,$E}' also will print the samething.

like image 121
RavinderSingh13 Avatar answered Sep 11 '25 01:09

RavinderSingh13