Set environment variables on Mac OS X Lion

后端 未结 16 1223
余生分开走
余生分开走 2020-11-22 11:01

When someone says \"edit your .plist file\" or \"your .profile\" or \".bash_profile\" etc, this just confuses me. I have no idea where these files are, how to create them if

相关标签:
16条回答
  • 2020-11-22 11:15

    Here's a bit more information specifically regarding the PATH variable in Lion OS 10.7.x:

    If you need to set the PATH globally, the PATH is built by the system in the following order:

    1. Parsing the contents of the file /private/etc/paths, one path per line
    2. Parsing the contents of the folder /private/etc/paths.d. Each file in that folder can contain multiple paths, one path per line. Load order is determined by the file name first, and then the order of the lines in the file.
    3. A setenv PATH statement in /private/etc/launchd.conf, which will append that path to the path already built in #1 and #2 (you must not use $PATH to reference the PATH variable that has been built so far). But, setting the PATH here is completely unnecessary given the other two options, although this is the place where other global environment variables can be set for all users.

    These paths and variables are inherited by all users and applications, so they are truly global -- logging out and in will not reset these paths -- they're built for the system and are created before any user is given the opportunity to login, so changes to these require a system restart to take effect.

    BTW, a clean install of OS 10.7.x Lion doesn't have an environment.plist that I can find, so it may work but may also be deprecated.

    0 讨论(0)
  • 2020-11-22 11:17

    I took the idiot route. Added these to the end of /etc/profile

    for environment in `find /etc/environments.d -type f`
    do
         . $environment
    done
    

    created a folder /etc/environments create a file in it called "oracle" or "whatever" and added the stuff I needed set globally to it.

    /etc$ cat /etc/environments.d/Oracle

    export PATH=$PATH:/Library/Oracle/instantclient_11_2
    export DYLD_LIBRARY_PATH=/Library/Oracle/instantclient_11_2
    export SQLPATH=/Library/Oracle/instantclient_11_2
    export PATH=$PATH:/Library/Oracle/instantclient_11_2
    export TNS_ADMIN=/Library/Oracle/instantclient_11_2/network/admin
    
    0 讨论(0)
  • 2020-11-22 11:18

    First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:

    ~/.bash_profile
    

    Update for Mavericks

    OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.

    Lion and Mountain Lion Only

    OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:

    ~/.MacOSX/environment.plist
    

    If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.

    Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).

    It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.

    One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.

    Edit: Here are some instructions for creating a plist file.

    1. Open Xcode
    2. Select File -> New -> New File...
    3. Under Mac OS X select Resources
    4. Choose a plist file
    5. Follow the rest of the prompts

    To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.

    Once the plist file is created you can open it with Xcode to modify it anytime you wish.

    0 讨论(0)
  • 2020-11-22 11:18

    More detail, which may perhaps be helpful to someone:

    Due to my own explorations, I now know how to set environment variables in 7 of 8 different ways. I was trying to get an envar through to an application I'm developing under Xcode. I set "tracer" envars using these different methods to tell me which ones get it into the scope of my application. From the below, you can see that editing the "scheme" in Xcode to add arguments works, as does "putenv". What didn't set it in that scope: ~/.MACOS/environment.plist, app-specific plist, .profile, and adding a build phase to run a custom script (I found another way in Xcode [at least] to set one but forgot what I called the tracer and can't find it now; maybe it's on another machine....)

    GPU_DUMP_DEVICE_KERNEL is 3

    GPU_DUMP_TRK_ENVPLIST is (null)

    GPU_DUMP_TRK_APPPLIST is (null)

    GPU_DUMP_TRK_DOTPROFILE is (null)

    GPU_DUMP_TRK_RUNSCRIPT is (null)

    GPU_DUMP_TRK_SCHARGS is 1

    GPU_DUMP_TRK_PUTENV is 1

    ... on the other hand, if I go into Terminal and say "set", it seems the only one it gets is the one from .profile (I would have thought it would pick up environment.plist also, and I'm sure once I did see a second tracer envar in Terminal, so something's probably gone wonky since then. Long day....)

    0 讨论(0)
  • 2020-11-22 11:20

    Step1: open ~/.bash_profile

    Now a text editor opens:

    Step2: variable name should be in capitals. in this example variable is NODE_ENV

    Step3: export NODE_ENV=development

    Save it and close.

    Restart your system.

    Done.

    To check env variable: open terminal and type

    echo $NODE_ENV 
    
    0 讨论(0)
  • 2020-11-22 11:23

    Setup your PATH environment variable on Mac OS

    Open the Terminal program (this is in your Applications/Utilites folder by default). Run the following command

    touch ~/.bash_profile; open ~/.bash_profile
    

    This will open the file in the your default text editor.

    For ANDROID SDK as example :

    You need to add the path to your Android SDK platform-tools and tools directory. In my example I will use "/Development/android-sdk-macosx" as the directory the SDK is installed in. Add the following line:

    export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools
    

    Save the file and quit the text editor. Execute your .bash_profile to update your PATH.

    source ~/.bash_profile
    

    Now everytime you open the Terminal program you PATH will included the Android SDK.

    0 讨论(0)
提交回复
热议问题