DevNull DevNull - 1 year ago 76
Linux Question

Enable LD_PRELOAD just for BASh after system startup

Is there a way to inject/enable LD_PRELOAD just for new sessions (ie:


I have a syntax highlighting library that I want to have automatically enabled (ie: highlight warnings for certain users), and just need it loaded for
rather than all processes. If I put it in
, it's disruptive and causes issues for all the system services and other programs that don't need it running, wrapping system calls (

Is there a simple way to accomplish this?

Answer Source

The easiest solution is probably to replace bash with a shell script that performs the LD_PRELOAD logic, then calls the actual (renamed) bash binary.

That is, move /bin/bash to /bin/bash.original, then create a script /bin/bash with the following contents:


exec /bin/bash.original "$@"

You could include logic here (e.g., "is stdout a tty") if you want to only perform the LD_PRELOAD when connected to an interactive session. Trying to perform any sort of terminal manipulation when bash isn't connected to a tty will probably yield weird results.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download