How to Override Binary Path for Specific User Without Modifying Original Scripts


2 views

Many developers encounter situations where they need to use an updated version of a binary while existing scripts continue pointing to the old version. Consider this common setup:

# Current system binary
/usr/bin/old_binary

# New binary location
/mount/new_version/new_binary

Here are several effective methods to redirect binary calls for your user account without modifying system scripts:

1. PATH Environment Variable Manipulation

Modify your shell configuration file (~/.bashrc, ~/.zshrc, etc.):

export PATH="/mount/new_version:$PATH"

This ensures your shell looks in the new location first. Verification:

which binary
# Should return /mount/new_version/binary

2. Alias Creation

For simpler cases, create an alias in your shell rc file:

alias binary="/mount/new_version/binary"

3. Function Overrides

For more complex scenarios where you need argument handling:

binary() {
    /mount/new_version/binary "$@"
}
export -f binary

4. Using execve Wrappers (Advanced)

Create a small C wrapper compiled to ~/bin/binary:

#include 
int main(int argc, char *argv[]) {
    char *new_args[argc+1];
    new_args[0] = "/mount/new_version/binary";
    for(int i=1; i

Compile with:

gcc wrapper.c -o ~/bin/binary

After implementing any solution, verify with:

type binary
# Should show your override implementation
binary --version
# Should show new version details
  • Make sure the new binary is truly compatible with the old one
  • Test extensively with the actual scripts
  • Consider file permissions on the new binary
  • Document your override for future reference

We've all encountered situations where legacy scripts hardcode binary paths, but we need to use updated versions without modifying the original scripts. Here's a comprehensive guide to solving this specifically for individual users.

When a script executes binary -doSomething, the shell searches for 'binary' in directories listed in the $PATH environment variable, in order. We can leverage this behavior.

echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

Add this to your ~/.bashrc or ~/.bash_profile:

export PATH="/mount/new_version:$PATH"

This ensures your custom path is searched first. Verify with:

which binary
/mount/new_version/binary

For more precise control, create an alias:

alias binary="/mount/new_version/binary"

Add this to your shell initialization file. Works well when you need to add specific flags:

alias binary="/mount/new_version/binary --special-flag"

When you need complex logic before calling the binary:

binary() {
    # Add pre-execution logic here
    /mount/new_version/binary "$@"
    # Add post-execution logic here
}
export -f binary

Create a personal ~/bin directory and link:

mkdir -p ~/bin
ln -s /mount/new_version/binary ~/bin/binary
export PATH="$HOME/bin:$PATH"

After implementing any solution, verify with:

type -a binary
binary is aliased to /mount/new_version/binary'
binary is /mount/new_version/binary
binary is /usr/bin/binary

Check script behavior remains unchanged except for using your preferred binary version.

  • Environment changes won't affect already running shells - start new sessions
  • Some applications modify PATH - check with env command
  • For systemd services, modify unit files instead
  • Consider permissions on the new binary location