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.

7 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

1 Like

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

Hello Folks

came to this Thread today - have to say thank you for creating Chris (@cdthomer)

since I have to deal with two Systems on two Machines, too (High Sierra & Catalina)

this Topic is very helpful....

I have built a little Macro for me which should do the job to show me every Directory Line by Line depending on the Machine it was run

It works like a charm on my MacBook Pro 2010 using the approach from @ccstone in Post #4 but it doesn't work on my iMac (Catalina) using @drdrang's approach in Post #2.

In Case you want to see how I built the Macro - Here it is

Download: [@$PATH] List all Paths whose contain Executables Line by Line.kmmacros (31,1 KB)

This Macro was built based on all Infos I got from this Thread - hopefully someone can chime in here to help me fixing the Issue.

Greetings from Germany

Tobias

1 Like

From man sed in Catalina:

  1. The escape sequence \n matches a newline character embedded in the pattern space. You cannot, however, use a literal newline character in
    an address or in the substitute command.

sed might be overkill anyway :slight_smile: Try echo $PATH | tr ':' '\n' -- it should work on both (all?) platforms too, so no need for the "If Then Else".

Hello Nige

thank you for providing this less complex example using the tr - command I had the man page in front of me yesterday because a family member pointed me to it saying "its worth to have this thing in your Toolbox for Shell scripting"....

I tested your approach with the piped tr command on both my Macs - it just works...

now to the piped sed command: how could I fix the issue using sed ??
I want to know this for learning.

Greetings from Germany

Tobias

It'll work if you use the "old" version above -- echo $PATH | sed $'s/:/\\\n/g'. That also works on Monterey, so it's the most portable sed version. Perhaps the "new" style that @drdrang offered came in with Big Sur, or maybe Monterey...

I'm afraid I don't know enough about how sed works to tell you why, but it must be something to do with explicitly setting the "address" to the last line of input (the $ in the command). I can glean that much from the man page -- but haven't a clue what it means!

1 Like

Hello Nige

thanks for pointing me in the right Direction - now I think that I better understand the two sed approaches..... the "old" style - originally posted here by @ccstone is the most common Version which will work on the most Systems out of the box - even many newer systems - because of the backwards compatibility for sed using that command only written in the old style to get every $PATH - Directory from the System as a List.

The Version from @drdrang will only work - if you have a macOS Version that also delivers the ability for the "Newer" Style with the updated/enhanced Version of sed. - And in this particular case this could be like you said Big Sur or Monterey.

Damn it - I knew it wasn't a good idea yesterday writing that Macro I posted above on a hurry....

Thank you so much Nige - you made this more clear to me - perhaps someone with more knowledge about the "Newer" Style will write here about it - that would be great but for now I hopefully have everything I need to know and everything I have written in this message is right.

Greetings from Germany

Tobias

Tobias - I can't really answer the question regarding different versions of sed but I can give you an alternative way to get the PATH environment variable as a list on any Mac with KM (which is the way I've been doing it for a while now...)

Test Get PATH.kmmacros (3.3 KB)

Click to see macro

Keyboard Maestro Export

Hello Taj

many thanks for this alternative solution using KM's native way. I have to say that I never thought of a way like this.... very nifty

Greetings from Germany to France

Tobias

1 Like