Bash – use a shebang to have a file source itself into current bash environment

bashshebangshellshell-script

I have a growing collection of scripts which should be sourced, not run. At the moment they have the shebang

#! /bin/cat

but I would prefer the have them be sourced into bash when run, in the same way as I had done

$ . /path/to/script.sh

or

$ source /path/to/script.sh

But . and source are bash builtins, so is an alternative shebang line for such scripts possible?

Best Answer

No. By the time a shebang comes into play, you have already lost. A shebang is applied when a process is exec()'d and typically that happens after forking, so you're already in a separate process. It's not the shell that reads the shebang, it's the kernel.

Related Question