Mac Leopardのbashに関する設定ファイル(.bash_profile .bashrc など)をまとめてみた

Mac LeopardFlex SDKを入れた時に環境変数のPATHをいじったけど、その時ちゃんと設定してないはずのMacPorts用のPATHも設定されてる事に気がつきました。
でもFlex SDK用のPATHを設定したら、MacPorts用の設定が消えてしまい、不思議に思ったので調べてみました。

結論

Flex SDK用のPATHを記述するために ~/.bash_profile を作成した事で、MacPortsインストーラーが用意した ~/.profile が読み込まれなくなった事が原因でした。
MacPortsはインストール後に自分でPATHに追加する必要があると説明する所もありましたが、恐らくもともと ~/.bash_profile か ~/.bash_login を作ってある環境だったからかも知れません。
もしくは、昔のMacPortsではそうだったのかも。

読み込み順序

UNIXベースなので基本はその辺と同じだと思います。
UNIX的なルールと、実験してみた結果を元に、予想も含みつつロードされる順番を整理してみました。

  1. /etc/profile
    • UNIXなので多分最初にロードされる
  2. /etc/paths
    • /etc/profile 内で /usr/libexec/path_helperが呼ばれ、その結果ロードされる
  3. /etc/paths.d/*
    • /etc/profile 内で /usr/libexec/path_helperが呼ばれ、その結果ロードされる
  4. /etc/bashrc
    • /etc/profile 内で 「. /etc/bashrc」 されている。
  1. ~/.bash_profile
    • /etc/profile から目では終えないが、UNIXなので多分ロードされる
  2. ~/.bash_login
    • ~/.bash_profile が存在しない場合、多分ロードされる
  3. ~/.profile
    • ~/.bash_login が存在しない場合、多分ロードされる
  4. ~/.bashrc
    • 上記3ファイル内から「source .bashrc」など、ちゃんと書いてロードする

独特だなと思ったのは大体こんな所です。

  • /etc/bashrc は /etc/profile 内から明示的にロードされている
  • ~/.bashrc はどこかでロードするよう書かないと、読み込まれない
  • /etc/paths という仕組み

MacPorts

MacPortsdmgイメージを使ってインストールすると、自動的にPATHを設定してくれます。
ただ、その方法が ~/.profile の作成なので、~/.bash_profile などが在ると読み込まれません。
その事は、MacPortsのインストールの解説にも書いてありました。
~/.bash_profile や ~/.bash_login を作るなら、自分でマージをしろとの事です。

The MacPorts binary installer places a .profile file in your home directory (view files preceded by a dot with 'ls -a'). The contents of this ~/.profile file adds the MacPorts paths in front of the standard Unix paths for the default BASH shell. This is done so that if you have utilities or libraries from both MacPorts and OS X's standard install, the MacPorts libraries will be run instead of the ones provided by Apple.

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
http://trac.macports.org/wiki/InstallingMacPorts#RequirementsfortheMacPortsshellenvironment

Other profile files may take precedence over ~/.profile. If you are using one of the following profile files in your home directory, you will need to merge the contents of it into ~./profile, or vice versa because the shell will ignore one of them:
~/.bash_login
~/.bash_profile

http://trac.macports.org/wiki/InstallingMacPorts#RequirementsfortheMacPortsshellenvironment

path_helper

path_helper については、凄くまとまってるエントリーがありました。

なんかちょっといい加減なコメントですが、ソースを読むと、/etc/pathsを書き換えるか/etc/paths.dにファイルを置くことでPATHを設定することができるようです。/etc/pathsファイルに優先度の高いパスを書き、/etc/paths.dディレクトリ以下に優先度の低いパスが書かれたファイルを置きます。path.d以下のファイルはアルファベット順に読み込まれます。

これはLeopardから追加されたパスの管理方法みたいです。

最近のMac OSXで、PATHをスマート(?)に管理するやり方。 - こせきの技術日記

環境

Mac Mac OS X 10.5.?*1Leopard

*1:いつだったか忘れました