in reply to How do I use 'which' in the hash-bang line?

I usually use

#!/usr/bin/env perl

which runs the (first) perl found in the PATH environment variable. See env.

Replies are listed 'Best First'.
Re^2: How do I use 'which' in the hash-bang line?
by davies (Prior) on Mar 15, 2018 at 15:06 UTC

    Note that env does not allow the passing of parameters to the target, so that #!/usr/bin/env perl -w does not work. At least, this was true the last time I tried.


    John Davies

      <update2> Sorry, I got so caught up in the point I was trying to make - that Perl scans the shebang line even when running the script as perl - that I missed the limitation that the AM pointed out below, that you are right that #!/usr/bin/env perl -w does in fact not work when invoking the script via ./ </update2>

      so that #!/usr/bin/env perl -w does not work

      It does for me:

      $ cat #!/usr/bin/env perl -wls print $x+undef; $ perl -x=5 Use of uninitialized value in addition (+) at line 2. 5

      From perlrun: 'The #! line is always examined for switches as the line is being parsed. ... Parsing of the #! switches starts wherever "perl" is mentioned in the line.'

      (I'm not sure about the history of this feature at the moment.) Update: The description of this feature was in perl-5.000, although apparently the support for which options are supported there has changed, for example perl58delta says "The command-line options -s and -F are now recognized on the shebang (#!) line." or perl5100delta says "The -C option can no longer be used on the #! line." (Although as choroba pointed out and as the docs say: "It wasn't working there anyway, since the standard streams are already set up at this point in the execution of the perl interpreter.")


        $ chmod a+x $ ./

        The env utility *supports* passing arguments to the command .... if invoked from command line (with args split by shell). As part of shebang, however, all following arguments are passed to env as a single string. So if you don't have a command such as "perl -wls" on the $PATH, the result is a failure.

Re^2: How do I use 'which' in the hash-bang line?
by rizzo (Curate) on Mar 15, 2018 at 17:56 UTC
    I usually use

    #!/usr/bin/env perl

    Is there any difference from using "which"?

    From the man page of "which" I would expect exactly the same behaviour:

    Which takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash(1).

    difference is run vs. print to stdout.

      which just prints it, env runs it. Big difference.

        Thanks for your answer. Really a big difference which I a found at last( and after making the script executable ;-) Oh Lord ... ).
Re^2: How do I use 'which' in the hash-bang line?
by rje (Deacon) on Mar 15, 2018 at 14:48 UTC
    I bet that was it! Thanks!