Search code examples
bashpathlldb

LLDB with Python problem: set PATH exclusively for one executable?


Is there a way of set the PATH variable exclusively for one executable in bash script?

I want to do so because somehow macOS's LLDB requires system-intalled Python, not my Anaconda-managed Python, therefore I need to ensure /usr/bin is at the beginning of PATH. But I prefer Anaconda-managed Python for everyday use, so I don't want to set PATH permanently just to accommodate LLDB.

Temporarily manually writing PATH before and after using LLDB is cumbersome, so I'm thinking about some kind of wrapper script or alias that automates this routine.

P.S. LLDB has the same problem with Homebrew-managed Python.


Solution

  • Environment variables are, by definition, per-process. Each process has a copy of the environment which it can modify for its own reasons.

    To override the PATH just for a single invocation, all sh-compatible shells allow you to say

    PATH=newvalue executable with arguments
    

    which sets PATH to newvalue for the duration of the execution of executable with arguments, then reverts the value back to its previous state (the current value, or unset if it was unset).

    If you want to override something in the environment every time you execute something, you need a wrapper. Assuming you have /usr/local/bin before /usr/bin in your PATH, you could install this in /usr/local/bin/something to override /usr/bin/something with a wrapper:

    #!/bin/sh
    PATH=newvalue
    exec /usr/bin/something "$@"
    

    Remember chmod a+x and of course you need to be root to have write access to this directory in the first place.

    For your private needs, a shell function in your .profile or similar is sufficient.

    something () {
        PATH=newvalue command something "$@"
    }