LoginSignup
1989
1797

More than 3 years have passed since last update.

本当に正しい .bashrc と .bash_profile の使ひ分け

Last updated at Posted at 2014-04-16

適当にググると「とにかく何でも .bash_profile に書いとけばおk」みたいな嘘を書いたブログ記事がわんさか出てくるのでここに正解を書いておきます。

.bash_profile

.bash_profile はログイン時にのみ実行されます。具体的な用途は:

  • 環境変数を設定する (export する変数)

環境変数はプロセス間で勝手に受け継がれるのでログイン時のみ設定すれば十分です。

.bashrc

.bashrc は対話モードの bash を起動する時に毎回実行されます。具体的な用途は:

  • 環境変数でない変数を設定する (export しない変数)
  • エイリアスを定義する
  • シェル関数を定義する
  • コマンドライン補完の設定をする

これらは bash を起動する度に毎回設定する必要があるものです。

その他

.bash_profile ? .bashrc ? いろいろあるけどこいつらなにもの? を参照。もっと突き詰めたい人は Linux: .bashrcと.bash_profileの違いを今度こそ理解する

Bash 以外のシェルを使用しない人は .bash_profile と .bashrc だけ知っておけば十分です。

.bashrc に書いた設定が反映されないけど .bash_profile に書くといける場合は、 .bash_profile から .bashrc を読み込む処理が抜けてます。

# .bash_profile の最後に
test -r ~/.bashrc && . ~/.bashrc
1989
1797
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1989
1797