title: shebang env preferred python version
body: <p>I have some python-2.x scripts which I copy between different systems, Debian and Arch linux.
  Debian install python as '/usr/bin/python' while Arch installs it as '/usr/bin/python2'.
  A problem is that on Arch linux '/usr/bin/python' also exists which refers to python-3.x.
  So every time I copy a file I have to correct the shebang line, which is a bit annoying.</p>
  
  <p>On Arch I use </p>
  
  <pre><code>#!/usr/bin/env python2
  </code></pre>
  
  <p>While on debian I have</p>
  
  <pre><code>#!/usr/bin/env python
  </code></pre>
  
  <p>Since 'python2' does not exist on Debian, is there a way to pass a preferred application? Maybe with some shell expansion? I don't mind if it depends on '/bin/sh' existing for example.
  The following would be nice but don't work.</p>
  
  <pre><code>#!/usr/bin/env python2 python
  #!/usr/bin/env python{2,}
  #!/bin/sh python{2,}
  #!/bin/sh -c python{2,}
  </code></pre>
  
  <p>The frustrating thing is that 'sh -c python{2,}' works on the command line: i.e. it calls python2 where available and otherwise python.</p>
  
  <p>I would prefer not to make a make a link 'python2->python' on Debian because then if I give the script to someone else it will not run. Neither would I like to make 'python' point to python2 on Arch, since it breaks with updates.</p>
  
  <p>Is there a clean way to do this without writing a wrapper? </p>
  
  <p>I realize similar question have been asked before, but I didn't see any answers meeting my boundary conditions :)
  <a href="https://stackoverflow.com/questions/12070516/conditional-shebang-line-for-different-versions-of-python">Conditional shebang line for different versions of Python</a></p>
  
  <p>--- UPDATE</p>
  
  <p>I hacked together an ugly shell solution, which does the job for now.</p>
  
  <pre><code>#!/bin/bash
  pfound=false; v0=2; v1=6
  for p in /{usr/,}bin/python*; do  
    v=($(python -V 2&gt;&amp;1 | cut -c 7- | sed 's/\./ /g'))
    if [[ ${v[0]} -eq $v0 &amp;&amp; ${v[1]} -eq $v1 ]]; then pfound=true; break; fi
  done
  if ! $pfound; then echo "no suitable python version (2.6.x) found."; exit 1; fi
  $p - $* &lt;&lt;EOF
  
  PYTHON SCRIPT GOES HERE
  
  EOF
  </code></pre>
  
  <p>explanation: 
  get version number (v is a bash array) and check</p>
  
  <pre><code>v=($(python -V 2&gt;&amp;1 | cut -c 7- | sed 's/\./ /g'))
  if [[ ${v[0]} -eq $v0 &amp;&amp; ${v[1]} -eq $v1 ]]; then pfound=true; break; fi
  </code></pre>
  
  <p>launch found program $p with input from stdin (-) and pass arguments ($*)</p>
  
  <pre><code>$p - $* &lt;&lt;EOF
  ...
  EOF
  </code></pre>