このエントリーをはてなブックマークに追加


.bashrcと.bash_profileの違い

Linuxの標準シェルとして採用されているbash(Bourne Again Shell)ですが設定ファイルとして以下の2つがあります。

  • .bash_profile
  • .bashrc

この2つの違い(使い方)について、以下に記述します。


関連記事

.bashrcと.bash_profileの説明

.bashrcbashを起動するたびに実行されます
.bash_profileログイン時に一度実行されます

実験

CentOSでは以下のように.bashrc, .bash_profileが記述されています。

.bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions

.bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

以下のように編集してテストしてみます。

.bashrc編集版

読み込まれた時、Read .bashrcと表示します。

# .bashrc
echo "Read .bashrc"

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions

.bash_profile編集版

読み込まれた時、Read .bash_profileと表示します。
また環境変数TESTを設定してあります。

# .bash_profile
echo "Read .bash_profile"

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
export TEST=sakura
export PATH

  1. GUIのログイン後、ターミナルを起動した場合
    Read .bash_profileと出力されていませんが、GUIによるログイン時に.bash_profileが動作しTEST=sakuraが設定されてることが確認できます。
    bash-profile-01.gif
     
  2. ターミナルアイコンをクリックして起動した場合
    Read .bashrcのみが表示されます。つまり、.bash_profileは読み込まれていません。
    bash-profile-02.gif
     
  3. sshにて接続しログインしてみる
    Read .bash_profile, Read .bashrcの両方が表示されます。
    つまり、ログイン時は.bash_profileが読み込まれていることが確認できました。
    .bash_profileの以下の構文により.bashrcが読み込まれ反映されます。
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
           . ~/.bashrc
    fi
    bash-profile-03.gif

 

添付ファイル: filebash-profile-02.gif 628件 [詳細] filebash-profile-03.gif 622件 [詳細] filebash-profile-01.gif 634件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2015-03-20 (金) 22:30:00