2

My env says:

_=/usr/bin/env

and I have a question regarding the shebang, will the entry be correct if I type:

#!_ perl

?

  • 1
    The only reason _ is set to "/usr/bin/env" is because you're using /usr/bin/env to show it. Try showing it with python instead, like python -c 'import os; print(os.getenv("_"))' or perl with perl -e 'print $ENV{"_"}."\n"' and it'll show the path to your python or perl executable instead. – Gordon Davisson Feb 20 '21 at 03:14

1 Answers1

5

No, it will not. In this case, the _ means the environment variable $_, which is typically set to the last parameter on the previous line. It is not universally /usr/bin/env.

However, a shebang does not expand environment variables. It consists of #!, an optional space, the name of a command, and at most a space and one argument (some systems allow more, but not all do). So the shebang will try to execute the program _, which is probably not what you intended.

Even if this did work, it would be confusing, and so you'd want to avoid it in clear code intended for use by others.

bk2204
  • 4,099
  • 7
  • 9
  • 1
    The shebang line doesn't use $PATH. Or maybe it does somewhere, but it's not a general behaviour. – choroba Feb 20 '21 at 00:38
  • In testing on Linux, it appears to, but I agree it's not documented, so I've updated the text. – bk2204 Feb 20 '21 at 00:43
  • Normally #!foo does not make a $PATH lookup on Linux. (This is literally the reason people use #!/usr/bin/env foo, relying on the 'env' utility to make a $PATH lookup.) Technically the OS will accept a relative name for the interpreter, like #!perl, but it's ... relative to your current cwd() at the time of execution, so not very useful at all. – u1686_grawity Feb 20 '21 at 10:18