On 2024-07-19 at 09:02, Michel Verdier wrote:

> On 2024-07-19, p...@gmx.it wrote:
> 
>> $ perl -le 'for( keys %ENV ){print "$_ --> $ENV{$_}"}' |grep perl
>> _ --> /usr/bin/perl
>>
>> the key for perl is "_" in environment variable? under this key, why
>> 'env perl' just works?
> 
> Perl $_ is the current (unnamed) value of your loop "for". You could
> write it like this:
> foreach my $key (keys %ENV) { print "$key=$ENV{$key}" }
> 
> https://perldoc.perl.org/variables

I think the question was not about the '$_' variable in the perl
expression, but about the '_' variable in the output, which in this case
contains the full path to the 'perl' binary.

$ env | grep ^_
_=/usr/bin/env

That appears to be a (shell?) environment variable. Greg commented on
it, as being a weird thing that you will probably never need to worry
about. I have done some experimenting with it, and it appears to hold
the equivalent of argv[0] for the command line which invoked the current
process - but the logic for determining what that is may well not be
intuitive for many people.

'echo $_' produces the full path to 'echo'.

'sh /tmp/testme' (where that file is a '#!/bin/sh' script which runs
'echo $_') produces the full path to 'sh'.

'bash /tmp/testme' (with the script shebang line still pointing to
/bin/sh) produces the full path to 'bash'.

'chmod +x /tmp/testme' followed by '/tmp/testme' produces '/tmp/testme'.


All in all, I think I agree with Greg's description.

-- 
   The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man.         -- George Bernard Shaw

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to