Can I have a conditional shebang?
I have a script that can be run with different interpreters:
#!/usr/bin/env default-interpreter
[my script]
But I would like to use an alternative-interpreter
if it's available, like this:
#!/usr/bin/env alternative-interpreter
[my script]
Is there a way to create a shebang that looks for alternative-interpreter
and falls back to default-interpreter
in case the first one isn't available?
Solution 1:
Not directly, no. Best to write a wrapper Bourne shell script and shebang that:
#!/path/to/my/wrapper
and the wrapper starts with:
#!/bin/sh
for shell in first second third; do
if /usr/bin/env "${shell}" "$@"; do exit $?; done
done
# We didn't find any of them.
exit 1
This lets env(1) use the ${PATH} search list to locate the programs, in the order given in the for-loop.