-1

I have this non-portable shebang:

#!/usr/bin/env python -u

It is non portable because python -u is fed as one single arg to env on my system.

Challenge: make this shebang portable changing the shebang only - that is to say a one-liner.

In other words, no solutions

Community
  • 1
  • 1
dnozay
  • 23,846
  • 6
  • 82
  • 104

1 Answers1

11

I'd use the following:

#!/bin/sh
"""true"
exec python -u "$0" "$@"
"""
# python code goes here

The line """true" will be parsed by sh as true, because it consists of an empty "" string followed by "true". Since true is a no-op command, it will be effectively ignored, and the following line will execute the Python interpreter.

On the other hand, Python will parse the """true" line very differently, as the opening of a triple-quoted string which starts with true" and is closed two lines below. Since the string is not used for anything, the Python interpreter will effectively ignore the shell snippet that starts up Python. It is the difference in interpretation of """xxx" that allows Python and sh code coexist in the same script.

For a simple test, append something like:

import sys
print "hello!", sys.argv

Given a reasonable sh implementation (and taking into account the time to start Python), this should not be measurably slower than using env.

user4815162342
  • 141,790
  • 18
  • 296
  • 355