Are shebang invocations detectable?

3

Is there a way for a bash script to detect if it's being run via the #!/usr/bin/env shebang?
Some magic bash foo that would let me write:

#!/bin/bash
...
if [ __INVOKED_VIA_USR_BIN_ENV__ ]; then 
  ...

aefxx

Posted 2014-05-24T23:25:02.453

Reputation: 133

Do you have access to the places where env is called? According to the man page the tool allows you to set environment variables in the call. – Marcus Rickert – 2014-05-24T23:34:13.340

No, not in this case. Unfortunately I cannot tinker with env as it is used in shebangs I do not control. Messing with the shebang would be reverted whenever the script updates itself. – aefxx – 2014-05-25T00:01:14.580

Answers

3

The last thing that env (from coreutils) does is

execvp (argv[optind], &argv[optind]);

This means that env gets replaced with the other program, so you can't detect who started it. You can find out more in the execvp man page.

Cristian Ciupitu

Posted 2014-05-24T23:25:02.453

Reputation: 4 515

Thanks for the links, Cristian. Seems like this is the final answer. – aefxx – 2014-05-25T00:55:16.617