この記事では、次のファイルが実行される順序について説明します:
- /etc/profile
- ~/.bash_profile
- ~/.bashrc
- ~/.bash_login
- ~/.profile
- ~/.bash_logout
インタラクティブ ログイン シェルの実行シーケンス
次の疑似コードは、これらのファイルの実行順序を説明しています。
execute /etc/profile IF ~/.bash_profile exists THEN execute ~/.bash_profile ELSE IF ~/.bash_login exist THEN execute ~/.bash_login ELSE IF ~/.profile exist THEN execute ~/.profile END IF END IF END IF
対話型シェルからログアウトすると、次の実行シーケンスが実行されます:
IF ~/.bash_logout exists THEN execute ~/.bash_logout END IF
以下に示すように、/etc/bashrc は ~/.bashrc によって実行されることに注意してください:
# cat ~/.bashrc if [ -f /etc/bashrc ]; then . /etc/bashrc fi
インタラクティブな非ログイン シェルの実行シーケンス
非ログイン インタラクティブ シェルの起動中の実行シーケンスは次のとおりです。
IF ~/.bashrc exists THEN execute ~/.bashrc END IF
注: 非対話型シェルが起動すると、ENV 環境変数が検索され、ENV 変数に記載されているファイル名の値が実行されます。
実行シーケンスをテストする
実行シーケンスをテストする方法の 1 つは、これらのファイルに異なる PS1 値を追加し、シェルに再ログインして、Linux プロンプトによって取得された PS1 値を確認することです。また、以前、PS1 を使用して Linux プロンプトを機能的かつスタイリッシュにする方法について説明しました。
1. /etc/profile が実行されます。 次の PS1 行を /etc/profile に追加し、再ログインして、Linux プロンプトが /etc/profile 内で設定された PS1 値に変更されることを確認します。
# grep PS1 /etc/profile PS1="/etc/profile> " [Note: re-login to see the prompt change as shown below] Last login: Sat Sep 27 16:43:57 2008 from 192.168.1.2 /etc/profile>
上記が正しく機能するためには、~/.bash_profile に PS1 がないことを確認してください。
<強い>2. ~/.bash_profile が実行されます: ~/.bash_profile、~/.bash_login、~/.profile、~/.bashrc に以下の PS1 を追加します。再ログインして、以下に示すように、Linux プロンプトが ~/.bash_profile 内で設定された PS1 値に変わることを確認します。
/etc/profile> grep PS1 ~/.bash_profile export PS1="~/.bash_profile> " /etc/profile> grep PS1 ~/.bash_login export PS1="~/.bash_login> " /etc/profile> grep PS1 ~/.profile export PS1="~/.profile> " /etc/profile> grep PS1 ~/.bashrc export PS1="~/.bashrc> " [Note: Upon re-login, it executed /etc/profile first and ~/.bash_profile next. So, it took the PS1 from ~/.bash_profile as shown below. It also did not execute ~/.bash_login, as ~/.bash_profile exists] Last login: Sat Sep 27 16:48:11 2008 from 192.168.1.2 ~/.bash_profile>
<強い>3. ~/.bash_login が実行されます。 .bash_profile の名前を別の名前に変更します。再ログインして、以下に示すように、Linux プロンプトが ~/.bash_login 内で設定された PS1 値に変わることを確認します。
~/.bash_profile> mv .bash_profile bash_profile_not_used [Note: Upon re-login, it executed /etc/profile first. Since it cannot find ~/.bash_profile, it executed ~/.bash_login] Last login: Sat Sep 27 16:50:55 2008 from 192.168.1.2 ~/bash_login>
<強い>4. ~/.profile が実行されます。 .bash_login の名前を別の名前に変更します。再ログインして、以下に示すように、Linux プロンプトが ~/.profile 内で設定された PS1 値に変わることを確認します。
~/.bash_login> mv .bash_login bash_login_not_used [Note: Upon re-login, it executed /etc/profile first. Since it cannot find ~/.bash_profile and ~/.bash_login, it executed ~/.profile] Last login: Sat Sep 27 16:56:36 2008 from 192.168.1.2 ~/.profile>
<強い>5. ~/.bashrc は非ログイン シェル テストのために実行されます . 「を実行中」 コマンド プロンプトで bash を実行すると、別の非ログイン シェルが表示され、以下に示すように .bashrc が呼び出されます。
~/.profile> bash [Note: This displays PS1 from .bashrc as shown below.] ~/.bashrc> exit exit [Note: After exiting from non-login shell, we are back to login shell] ~/.profile>
この記事が気に入ったら、del.icio.us でブックマークしてください そしてつまずく .