The first thing I do whenever I start a script is to add the #! line. When writing Python I have always written ‘#!/usr/bin/env python’ and when writing Perl ‘#!/use/bin/perl’. I suspect this stems from using systems where it was commonplace to have multiple versions of Python installed (at the time, 2.3 and 2.4) but only a single version of Perl (5.8.8).

This has always worked fine for me because 90% of the time there is only one version of Perl, and it(or a link to it) is at /usr/bin/perl. I discovered today, however, a situation where there are multiple versions of Perl and the one I want is not at /usr/bin/perl.

I recently purchased a 12″ PowerBook running OSX 10.5. I needed some development libraries installed and MacPorts seemed to offer the quickest, easiest and most maintainable route to install them. So I installed the libraries I needed and one of them pulled in the MacPorts version of Perl. I then spent the next 30 minutes, or so, trying to figure out why my script would not run but manually invoking the parts causing it to fail with ‘perl -e’ worked fine. It turned out (as you have probably guessed by now) that my script had a #! line using /usr/bin/perl (the OS provided version) whilst `perl` was using /opt/local/bin/perl (the MacPorts version) due to path precedence. Changing the #! line to ‘#!/usr/bin/env perl’ made the script run properly.

3 thoughts on “/usr/bin/env”

  1. Unfortunately, you may have problems if you need to use perl command-line switches in your scripts. I hope none of them need to use taint mode, for example, because there’s no other way of turning it on.

  2. When I ran ‘/usr/bin/env perl –version’ it seemed to just pass the switch through to perl.

    I perhaps should be using taint as I’m parsing emails which could come from untrusted sources but I’m not at the moment.

  3. Maybe it works on Mac OS X. :) Unless you are just running it from the command line, which is different? It should only affect shebang lines.

    $ cat foo
    #!/usr/bin/env perl -Tw
    print “Hello, World\n”;
    $ ./foo
    /usr/bin/env: perl -Tw: No such file or directory

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>