Altering environment variables

Discussion in 'OS X Mountain Lion (10.8)' started by msjones, Nov 18, 2012.

  1. msjones macrumors 6502

    msjones

    Joined:
    Oct 18, 2007
    Location:
    Nottinghamshire, UK
    #1
    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.
     
  2. switon, Nov 18, 2012
    Last edited: Nov 28, 2012

    switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #2
    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
     
  3. msjones thread starter macrumors 6502

    msjones

    Joined:
    Oct 18, 2007
    Location:
    Nottinghamshire, UK
    #3
    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.
     
  4. switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #4
    re: glad it works for you...

    You are so very welcome ... glad to be of service. Switon
     
  5. throttlemeister macrumors 6502a

    Joined:
    Mar 31, 2009
    Location:
    Netherlands
    #5
    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?
     
  6. switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #6
    RE: recursive redefines of the PATH variable...

    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
     
  7. throttlemeister macrumors 6502a

    Joined:
    Mar 31, 2009
    Location:
    Netherlands
    #7
    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.
     
  8. switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #8
    RE: thanks...

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

    Thanks throttlemeister,
    Switon
     

Share This Page