Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

msjones

macrumors 6502
Original poster
Oct 18, 2007
429
4
Nottinghamshire, UK
Hey guys, wondering if someone here may be able to help me. I have a little issue with setting my $PATH variable.

I have installed homebrew and need to set my PATH variable to begin with /usr/local/bin:/usr/local/sbin, which shouldnt be a problem. I added the following line to my /etc/launchd.conf file:

Code:
setenv PATH /usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

However when I reboot my PATH is set too /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin, not what I manually set it to in launchd.conf.

Any ideas on why this isn't taking? Its adding the extra to bin paths to rhe variable, but at the end rather than the beginning.

Thanks.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE PATH variable setting...

Hi msjones,

(Removed as the script was too complicated for what the OP wanted to have done. The "setenv" terminal command is all that is needed.)

Regards,
Switon
 
Last edited:

msjones

macrumors 6502
Original poster
Oct 18, 2007
429
4
Nottinghamshire, UK
Thanks for the script man, it does what I'm looking to do.

I had to create my launchd.conf file. It's a shame this way doesn't work but thanks to your script I have a work around.

Your help is appreciated.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
re: glad it works for you...

You are so very welcome ... glad to be of service. Switon
 

throttlemeister

macrumors 6502a
Mar 31, 2009
550
63
Netherlands
Why so complicated when you can just do 'export PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH:$HOME/bin'? Seems to be a very complicated means of setting a simple path?
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: recursive redefines of the PATH variable...

Why so complicated when you can just do 'export PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH:$HOME/bin'? Seems to be a very complicated means of setting a simple path?

Hi throttlemeister,

Concise reason: recursive redefines can cause trouble.

Yes, you might consider these shell commands complicated, but once written they work in all situations. For instance, sometimes you might wish to add a path to the beginning of the PATH variable, perhaps so that a different version of some utility will run instead of the standard version (say you wrote your own version of the utility which you want to run sometimes but not all the time). You can then prepend the new path, and if it is already there it won't be repeated.

Because you might have multiple shell scripts calling each other, say for init purposes, and each shell script might modify the PATH variable, you don't always know what PATH contains but you still might want to addend or prepend a new path to it. These shell commands do this. Also, say you have an initialization shell script that you use at the start of numerous other shell scripts to initialize certain variables before the work of the script is executed, and say that some of your "work" scripts can call others of your "work" scripts, then you might end up calling your initialization script many times (dozens of times, in my case). Now you could do a self-referencial $PATH redefine as you indicate, but then your PATH variable would expand with each execution of the initialization script and you would end up with a PATH with dozens of copies of the same paths all concatenated. Is this a problem? -- well, probably not so much today, but when this command was written it was a concern because the PATH variable had a relatively small finite size on the particular OS for which it was originally written and thus you didn't want to overrun the variable length causing an error. It is just good programming practice.

Now you could write shell commands into your initialization script so that it is only executed once in a single process and its subprocesses even though it is called multiple times, but this is essentially all that the prepend and append commands are doing, in the particular case of the PATH variable. This is actually the reason why these commands were written in the first place, and many years ago I gave them to friends who gave them to friends who gave them to friends and after about a year someone gave them back to me not realizing from where they had come. And there were a few minor modifications along the way, but this type of shell command strategy has ended up in all kinds of places, especially OS init routines, the GNU Project, Linux distros, books, tutorials, etc.

...sorry for the longwinded explanation...in a nutshell, self-referencial redefines can lead to problems...

Regards,
Switon
 

throttlemeister

macrumors 6502a
Mar 31, 2009
550
63
Netherlands
I understand what you are trying to say, but..

- A shell script modifying something like the PATH, only does so for its own shell.
- If you are calling multiple scripts from within the same shell, you should be aware of what those scripts are doing, if/what variables they need to inherit and how they are called.
- Script specific variables should be set from within the script and discarded after it finishes. You should not ever set global system variables just because some script needs it.

It's a nicely written solution, but being used to do doing things a certain way as a unix admin for years, to me it's a solution for a problem that doesn't really exists.

I appreciate you taking the time to explain and showing the script though; it can be a useful reference.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: thanks...

- A shell script modifying something like the PATH, only does so for its own shell. AND subprocesses...thereby making it useful since the values are inherited by all child processes.
- If you are calling multiple scripts from within the same shell, you should be aware of what those scripts are doing, if/what variables they need to inherit and how they are called. BUT, what if an one script might be called from any other script(s), and they all might need certain variables initialized? To solve this problem you call an initialization script at the top of all of your "work" scripts so that whenever and however any single work script is called, the necessary variables are initialized. (See many of the Linux init.d scripts for examples, such as networking. These scripts might be called from multiple places at multiple various times and by multiple users, so their proper initializations are important.)
- Script specific variables should be set from within the script and discarded after it finishes. You should not ever set global system variables just because some script needs it. AND, once again, if numerous variables need to be initialized, say by hundreds of shell command lines, then you could copy all of those lines to every one of your "work" scripts, or you could include them all in a single initialization script that is called at the head of every one of your "work" scripts. Since the variables are local to the process and its subprocesses, or passed from parent to child, they are discarded when all scripts finish.

I appreciate you taking the time to explain and showing the script though; it can be a useful reference. AND I too appreciate your time throttlemeister...

My underlined comments are added to the end of your items above...

Thanks throttlemeister,
Switon
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.