Relative shebang: How to write an executable script running portable interpreter which comes with it

Question:

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).

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.

The given constraints exclude both known approaches like shebang with absolute path:

#!/usr/bin/python 

and search in the environment

#!/usr/bin/env python

Separate launchers look ugly and are not acceptable.

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: http://www.in-ulm.de/~mascheck/various/shebang/

And I also found practical solutions for most of the languages with ‘multi-line shebang’ tricks. It allows to write scripts like this:

#!/bin/sh
"exec" "`dirname $0`/python2.7" "$0" "$@"
print copyright

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 --executable option which basically allows to specify the shebang content for the scripts it produces:

python setup.py build --executable=/opt/local/bin/python

So, in particular, what can be specified for --executable= 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…

The question

How to write a shebang which specifies an interpreter with a path which is relative to the location of the script being executed?

Asked By: Anton

||

Answers:

The relative path written directly in a shebang is treated relative to the current working directory, so something like #!../bin/python2.7 will not work for any other working directory except few.

Since OS does not support it, why not to use external program like using env for PATH lookup. But I know no specialized program which computes the relative paths from arguments and executes the resulting command.. except the shell itself and other scripting engines.

But trying to compute the path in a shell script like

#!/bin/sh -c '`dirname $0`/python2.7 $0'

does not work because on Linux shebang is limited by one argument only. And that suggested me to look for scripting engines which accept a script as the first argument on the command line and are able to execute new process:

Using AWK

#!/usr/bin/awk BEGIN{a=ARGV[1];sub(/[a-z_.]+$/,"python2.7",a);system(a"t"ARGV[1])}

Using Perl

#!/usr/bin/perl -e$_=$ARGV[0];exec(s/w+$/python2.7/r,$_)

update from 11Jan21:

Using updated env utility:

$ env --version | grep env
env (GNU coreutils) 8.30
$ env --help
Usage: env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
Set each NAME to VALUE in the environment and run COMMAND.

Mandatory arguments to long options are mandatory for short options too.
  -i, --ignore-environment  start with an empty environment
  -0, --null           end each output line with NUL, not newline
  -u, --unset=NAME     remove variable from the environment
  -C, --chdir=DIR      change working directory to DIR
  -S, --split-string=S  process and split S into separate arguments;
                        used to pass multiple arguments on shebang lines

So, passing -S to env will do the job

Answered By: Anton

The missing "punchline" from Anton’s answer:

With an updated version of env, we can now realize the initial idea:

#!/usr/bin/env -S /bin/sh -c '"$(dirname "$0")/python3" "$0" "$@"'

Note that I switched to python3, but this question is really about shebang – not python – so you can use this solution with whatever script environment you want. You can also replace /bin/sh with just sh if you prefer.

There is a lot going on here, including some quoting hell, and at first glance it’s not clear what’s happening. I think there’s little worth to just saying "this is how to do it" without explanation, so let’s unpack it.

It breaks down like this:

  1. The shebang is interpreted to run /usr/bin/env with the following arguments:

    1. -S /bin/sh -c '"$(dirname "$0")/python3" "$0" "$@"'
    2. full path (either local or absolute) to the script file
    3. onwards, any extra commandline arguments
  2. env finds the -S at the start of the first argument, and splits it according to (simplified) shell rules. In this case, only the single-quotes are relevant – all the other fancy syntax is within single-quotes so it gets ignored. The new arguments to env become:

    1. /bin/sh
    2. -c
    3. "$(dirname "$0")/python3" "$0" "$@"
    4. full path to script file (either local or absolute)
    5. onwards, (possibly) extra arguments
  3. It runs /bin/sh – the default shell – with the arguments:

    1. -c
    2. "$(dirname "$0")/python3" "$0" "$@"
    3. full path to script file
    4. onwards, (possibly) extra arguments
  4. As the shell was run with -c, it runs in the second operating mode defined here (and also re-described many times by different man pages of all shells, e.g. dash, which is much more approachable). In our case we can ignore all the extra options, the syntax is:

     sh -c command_string command_name [argument ...]
    

    In our case:

    1. command_string is "$(dirname "$0")/python3" "$0" "$@"
    2. command_name is the script path, e.g. ./path to/script dir/script file.py
    3. argument(s) are any extra arguments (it’s possible to have zero arguments)
  5. As described, the shell wants to run command_string ("$(dirname "$0")/python3" "$0" "$@") as a command, so now we turn to the Shell Command Language:

    • Parameter Expansion is performed on "$0" and "$@", which are both Special Parameters:

      • "$@" expands to the argument(s). If there were no arguments, it will "expand" into nothing. Because of this special behaviour, it’s explained horribly in the spec I linked, but the man page for dash explains it much better.
      • $0 expands to command_name – our script file. Every occurrence of $0 is within double-quotes so it doesn’t get split, i.e. spaces in the path won’t break it up into multiple arguments.
    • Command Substitution is applied, substituting $(dirname "$0") with the standard output of running the command dirname "./path to/script dir/script file.py", i.e. the folder that our script file resides in: ./path to/script dir.

    After all of the substitutions and expansions, the command becomes, for example:

     "./path to/script dir/python3" "./path to/script dir/script file.py" "first argument" "second argument" ...
    
  6. Finally, the shell runs the expanded command, and executes our local python3 with our script file as an argument followed by any other arguments we passed to it.

Phew!


What follows is basically my attempts to demonstrate that those steps are occuring. It’s probably not worth your time, but I already wrote it and I don’t think it’s so bad that it should be removed. If nothing else, it might be useful to someone if they want to see an example of how to reverse-engineer things like this. It doesn’t include extra arguments, those were added after Emanuel’s comment.

It also has a lousy joke at the end..


First let’s start simpler. Take a look at the following "script", replacing env with echo:

$ cat "/home/neatnit/Projects/SO question 33225082/my script.py"
#!/usr/bin/echo  -S  /bin/sh  -c  '"$(  dirname  "$0"  )/python2.7"  "$0"'

print("This is python")

It’s hardly a script – the shebang calls echo which will just print whichever arguments it’s given. I’ve deliberately put two spaces between the words, this way we can see how they get preserved. As an aside, I’ve deliberately put the script in a path that contains spaces, to show that they are handled correctly.

Let’s run it:

$ "/home/neatnit/Projects/SO question 33225082/my script.py"
-S  /bin/sh  -c  '"$(  dirname  "$0"  )/python2.7"  "$0"' /home/neatnit/Projects/SO question 33225082/my script.py

We see that with that shebang, echo is run with two arguments:

  1. -S /bin/sh -c '"$( dirname "$0" )/python2.7" "$0"'
  2. /home/neatnit/Projects/SO question 33225082/my script.py

These are the literal arguments echo sees – no quoting or escaping.

Now, let’s get env back but use printf [1] ahead of sh to explore how env processes these arguments:

$ cat "/home/neatnit/Projects/SO question 33225082/my script.py"
#!/usr/bin/env  -S  printf  %sn  /bin/sh  -c  '"$(  dirname  "$0"  )/python2.7"  "$0"'

print("This is python")

And run it:

$ "/home/neatnit/Projects/SO question 33225082/my script.py"
/bin/sh
-c
"$(  dirname  "$0"  )/python2.7"  "$0"
/home/neatnit/Projects/SO question 33225082/my script.py

env splits the string after -S [2] according to ordinary (but simplified) shell rules. In this case, all $ symbols were within single-quotes, so env did not expand them. It then appended the additional argument – the script file – to the end.

When sh gets these arguments, the first argument after -c (in this case: "$( dirname "$0" )/python2.7" "$0") gets interpreted as a shell command, and the next argument acts as the first parameter in that command ($0).

Pushing the printf one level deeper:

$ cat "/home/neatnit/Projects/SO question 33225082/my script.py"
#!/usr/bin/env  -S  /bin/sh  -c  'printf  %s\n  "$(  dirname  "$0"  )/python2.7"  "$0"'

print("This is python")

And running it:

$ "/home/neatnit/Projects/SO question 33225082/my script.py"
/home/neatnit/Projects/SO question 33225082/python2.7
/home/neatnit/Projects/SO question 33225082/my script.py

At last – it’s starting to look like the command we were looking for! The local python2.7 and our script as an argument!

sh expanded $0 into /home/[ ... ]/my script.py, giving this command:

"$(  dirname  "/home/[ ... ]/my script.py"  )/python2.7"  "/home/[ ... ]/my script.py"

dirname snips off the last part of the path to get the containing folder, giving this command:

"/home/[ ... ]/SO question 33225082/python2.7"  "/home/[ ... ]/my script.py"

To highlight a common pitfall, this is what happens if we don’t use double-quotes and our path contains spaces:

$ cat "/home/neatnit/Projects/SO question 33225082/my script.py"
#!/usr/bin/env  -S  /bin/sh  -c  'printf  %s\n  $(  dirname  $0  )/python2.7  $0'

print("This is python")
$ "/home/neatnit/Projects/SO question 33225082/my script.py"
/home/neatnit/Projects
.
33225082
./python2.7
/home/neatnit/Projects/SO
question
33225082/my
script.py

Needless to say, running this as a command would not give the desired result. Figuring out exactly what happened here is left as an exercise to the reader 🙂

At last, we put the quote marks back where they belong and get rid of the printf, and we finally get to run our script:

$ "/home/neatnit/Projects/SO question 33225082/my script.py"
/home/neatnit/Projects/SO question 33225082/my script.py: 1: /home/neatnit/Projects/SO question 33225082/python2.7: not found

Wait, uh, let me fix that

$ ln --symbolic $(which python3) "/home/neatnit/Projects/SO question 33225082/python2.7"
$ "/home/neatnit/Projects/SO question 33225082/my script.py"
This is python

Rejoice!


[1] This way we can see each argument in a separate line, and we don’t have to get confused by space-delimited arguments.

[2] There doesn’t need to be a space after -S, I just prefer the way it looks. -Sprintf sounds really exhausting.

Answered By: NeatNit
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.