I'm developing a perl script which expected to be downloaded by Mac users with a very small knowledge of shell, linux etc, let's say office managers and accountants.

After the downloading the script should be executed just by double-clicking via GUI.

My goal is to make this as painless as possible to non-tech-savvy user.

My doubts are:

  • after the downloading the script won't have the executable bit
  • if the perl executable is not at the default location then I should write something instead of #!/usr/bin/perl. What should I write there? Is there any other way except open a console and type perl ./script.pl ?
  • 5
    Does env come standard on OSX? If so, you could use #!/usr/bin/env perl which searches the path to find the perl executable.
    – John1024
    Feb 5, 2014 at 20:00
  • 1
    See perldoc perlrun Feb 5, 2014 at 20:08
  • @John1024, yes - env comes standard with OSX.
    – grebneke
    Feb 5, 2014 at 20:16

2 Answers 2


Or, you can have sh take care of it for you:

 exec perl -x "$0" "$@"


Yes, that's sh and Perl all in one file.

From man perlrun:

tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first line that starts with "#!" and contains the string "perl". Any meaningful switches on that line will be applied.

This approach only assumes the path of sh (which should be the same on any POSIX-compliant OS) and that a non-interactive instance of sh has perl somewhere in its PATH.

As for ensuring the script has the executable bit set, you can always distribute it as a tarball and have your users "right click, extract here" from the GUI. If the tarball contained the script with the executable bit set, the extracted script should have the executable bit set.

  • 1
    Note that POSIX doesn't specify the path of sh. /bin/sh may also be a Bourne shell (not on OS/X though) where that syntax should also work except in very old versions where you need ${1+"$@"} instead of "$@" (${1+"$@"} will also work in POSIX shells, but not in some old versions of zsh (/bin/sh was zsh on OS/X at some point)). Feb 7, 2014 at 13:46
  • @StephaneChazelas "POSIX doesn't specify the path of sh". That's very weird. Thanks for the info, though.
    – Joseph R.
    Feb 7, 2014 at 21:19

For the shebang line, use:

#!/usr/bin/env perl

env will search the PATH at execute the first perl that it finds. If you want to specify any options for perl, just add them to the end of the line above.

If more than one perl is installed, env will run the first one it finds in PATH.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .