Best Method to Set ENV_PATH Variable for Multiple Computers and Scripts?

Howdy folks, hoping to get some advice here on setting the ENV_PATH variable for multiple devices and scripts.

For instance, I run a variety of shell scripts on my iMac, both OS scripts and home brew scripts, as well as on my wife's MacBook, also OS and home brew. My iMac is running Big Sur, while the MacBook is running Monterey.

The path for OS shell scripts is apparently different for the two OS (no surprise), so initially I was just running a subroutine before any shell script action that would set the variable according to the device's UUID to differentiate between my iMac and her MacBook. However, now that I run home brew scripts on both devices, it has added a level of complexity in defining where the heck the proper path is for any given shell script.

So I'm hoping to get some ideas as to the best way to set the ENV_PATH variable depending on what device and what kind of script I'm running. I was thinking of having the subroutine first use a switch action to ID which device I'm on using the same %MacUUID% token I'm currently using, and then inside of that switch action, another switch that will run according to the %TriggerValue% token. For instance, I could supply the token of homebrew to indicate that the script is a homebrew script and needs to use that path, and leave the parameter empty to run OS scripts.

But since I'm still relatively new at running shell scripts, and don't want to screw anything up I'm hoping some of the more knowledgable folks can chime in with their ideas. Any help is appreciated!

-Chris

Here’s how I came up with an ENV_PATH to work on all my computers:

  1. On each computer, run

    echo $PATH | sed 's/:/\n/g'
    

    This will give you list of the directories that are searched for executables on that computer. The sed part isn’t necessary, but it breaks the list up onto separate lines, which makes the list easier to read.

  2. Looking at lists from all your computers, assemble a new list that is a superset of all the directories. In this list, the directories must be separated by colons, not newlines.

  3. Save this new list as ENV_PATH in Keyboard Maestro on all your computers.

The order of the list matters, because the directories are searched in that order. So if you’ve installed a version of, say, Perl that’s more recent than the one that comes with the Mac, you’ll want to make sure the directory it’s in comes before /usr/bin.

Don’t worry if the list contains directories that don’t exist on all the computers. When the system encounters a non-existing directory, it just moves on to the next one in the list.

5 Likes

This is awesome! I just ran it on my iMac and got the exact list I had initially set my variable to earlier this year when I started working with shell scripts. Later on today I'll run it on the MacBook and update my variable, thanks so much!

1 Like

This won't work on Mojave and before, because the ancient version of sed on those systems won't allow the newline metacharacter.

gsed manages that perfectly well, although that has to be installed by the user.

Here's a fix to use with older versions of “Apple” sed:

echo $PATH | sed $'s/:/\\\n/g'

I do not know for certain this will work on versions of macOS later than Mojave, but I would think so due to the nature of the C string.

-Chris

Hey Chris,

Drang's advice is good.

Don't overcomplicate things...  :sunglasses:

-ccs

1 Like

Just wanted to chime in and mention this came in handy today when I was helping somebody set up their ENV_PATH variable. Thanks again for sharing!

2 Likes