title: Relative shebang: How to write an executable script running portable interpreter which comes with it
body: <p>Let's say we have a program/package which comes along with its own interpreter and a set of scripts which should invoke it on their execution (using shebang).</p>
  
  <p>And let's say we want to keep it portable, so it remains functioning even if simply copied to a different location (different machines) without invoking setup/install or modifying environment (PATH). A system interpreter should not be mixed in for these scripts.</p>
  
  <p>The given constraints exclude both known approaches like shebang with absolute path:</p>
  
  <pre><code>#!/usr/bin/python 
  </code></pre>
  
  <p>and search in the environment</p>
  
  <pre><code>#!/usr/bin/env python
  </code></pre>
  
  <p>Separate launchers look ugly and are not acceptable.</p>
  
  <p>I found good summary of the shebang limitations which describe why relative path in the shebang are useless and there cannot be more than one argument to the interpreter: <a href="http://www.in-ulm.de/~mascheck/various/shebang/" rel="noreferrer">http://www.in-ulm.de/~mascheck/various/shebang/</a></p>
  
  <p>And I also found <a href="http://rosettacode.org/wiki/Multiline_shebang" rel="noreferrer">practical solutions</a> for most of the languages with 'multi-line shebang' tricks. It allows to write scripts like this:</p>
  
  <pre><code>#!/bin/sh
  "exec" "`dirname $0`/python2.7" "$0" "$@"
  print copyright
  </code></pre>
  
  <p>But sometimes, we don't want to extend/patch existing scripts which rely on shebang with an absolute path to interpreter using this approach. E.g. Python's setup.py supports <code>--executable</code> option which basically allows to specify the shebang content for the scripts it produces:</p>
  
  <pre><code>python setup.py build --executable=/opt/local/bin/python
  </code></pre>
  
  <p>So, in particular, what can be specified for <code>--executable=</code> in order to enable the desired kind of portability? Or in other words, since I'd like to keep the question not too specific to Python...</p>
  
  <h1>The question</h1>
  
  <p>How to write a shebang which specifies an interpreter with a path which is relative to the location of the script being executed?</p>