Dynamically choosing the interpreter

Hi,

Is it possible to choose the inerpreter conditionally.
For example, if

whereis bash 

returns

/usr/bin/bash

then i need to choose

#!/usr/bin/bash

else i need to use

#!/usr/bin/sh

.

Is it possible to achieve in a shell script?

Thanks

That's not possible, I'm afraid, you can use /usr/bin/env as shebang to find the user's preferred command, or rather, the command in the user's environment. But you can't "change" the shebang dynamically.

I'd go for something like this

#!/usr/bin/env sh

mybash=$(command -v bash 2>/dev/null)
## maybe switch to bash
if test "${0}" != "bash" -a -n "${mybash}"; then
  exec command bash $0 $@
fi

or something along the lines of that.