If, when I run a script, I use LD_PRELOAD
to designate a library to preload, I find that the library is in fact preloaded only if the script has a shebang line. For example, given this script:
# Not a shebang
echo Hello
and this command:
LD_PRELOAD=/path/to/preload_me.so ./script.sh
the script runs without the library being loaded at all, which I can monitor via the (non-)effects of its initialization code.
On the other hand, if I add a shebang line:
#!/bin/sh
echo Hello
... then the library is loaded when I run the script via the same command. It doesn't seem to matter exactly which interpreter is specified; certainly I can also use /bin/bash
or any other sh
-family shell I have tried.
Why is there a difference, and is there any way to ensure that a given library is preloaded before a given shell simple command, regardless of the command?
(Adapted from another question whose author resisted the question being couched in these terms.)