Dienstag, 28. Oktober 2014

$PATH Umgebungsvariable / Shell-Variable unter Mac OS X setzen

Traditioneller Weg

Unter dem Mac OS X gibt es traditionell drei Varianten die $PATH-Variable zu setzen

0. $PATH anzeigen
 

echo "$PATH"

I. /etc/paths

Bei dieser Variante wird die zentrale Datei /etc/paths editiert. In dieser Datei stehen alle Pfade, die zur PATH-Variable hinzugefügt werden sollen, zeilenweise unter einander. Um einen Pfad zu ergänzen, müssen folgende Schritte befolgt werden:

1. Schritt: Öffnen eines Terminal-Fensters

2. Schritt: Datei im Editor öffnen

1
$ sudo vi /etc/paths

3. Schritt: Den gewünschten Path als neue Zeile anhängen

4. Schritt: Datei speichern

Der neue Pfad steht jetzt in einer neuen Shell zur Verfügung.

Hinweis: Setzt die Pfade global für alle User!

II. /etc/paths.d

Stellt eine Abwandlung von Variante I. dar, die ich jedoch nicht unterschlagen möchte. Anstelle einer Datei wie /etc/paths, die alle Pfade beinhaltet, können Pfade in eigenen Dateien gruppiert werden. Dies macht die Administration der Pfade übersichtlicher. Dazu werden Dateien mit den gewünschten Pfad-Angaben (wie /etc/paths) in dem Verzeichnis /etc/paths.d angelegt. In unserem Beispiel verwenden wir eine Datei namens paths-example.  Hier macht es Sinn, der jeweiligen Datei den Namen der gewünschten Grupperierung zu geben, z.B. paths-databases.

1. Schritt: Öffnen eines Terminal-Fensters

2. Schritt: In Pfad wechseln

1
$ cd /etc/paths.d

3. Schritt: Eigene Pfad-Datei erzeugen

1
$ touch paths-example

4. Schritt: Den gewünschten Path als neue Zeile anhängen

5. Schritt: Datei speichern

Hinweis: Setzt die Pfade global für alle User!

III. .profile (bzw. .bash_profile s.u. Mavericks)

Wer den Standard-Weg bevorzugt, setzt den Pfad manuell in der Datei .profile:

1. Schritt: Öffnen eines Terminal-Fensters

2. Schritt: Ist Datei .profile nicht um User-Home-Verzeichnis?

1
$ touch .profile

3. Schritt: Datei im Editor öffnen

1
$ vi .profile

4. Schritt: PATH-Variable setzen:

1
export PATH=$PATH:/mein/neuer/pfad

4. Schritt: Datei speichern

Der neue Pfad steht jetzt in einer neuen Shell zur Verfügung.

Hinweis: Setzt die Pfade nur für den User, in dessen Homeverzeichnis die .profile angepasst wird!


Update for Mavericks & Yosemite

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. 

Quellen:
http://www.systemfeld.de/2011/11/29/setzen-der-path-umgebungsvariable-unter-mac-os-x/
http://stackoverflow.com/questions/7501678/set-environment-variables-on-mac-os-x-lion

Keine Kommentare :

Kommentar veröffentlichen