2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

~/.bash_profile と~/.bashrc と/etc/profile と~/.profile

Last updated at Posted at 2025-08-17

この記事はシェルの設定ファイルである~/.bash_profile~/.bashrc/etc/profile~/.profileの違いを調べたものである。

いつ誰が何を読み込むのか

以下に図示しているのだが、Qiitaだと文字が小さくなってしまった。新しいタブで画像を開くと見やすくなる。

どれに何を書くべきか

色々トラブった結果、自分用に何か環境変数を追加するときは~/.bashrcが安定だと思う。

/etc/profile

システム全体で使用する設定ファイル。全てのユーザーに共通の設定を行いたい場合はこのファイルに記述する。

1 システムに SSH で 100 人のユーザーがアクセスする場合役に立つのかもしれないが、1 システム 1 ユーザーの使い方でこのファイルを書き換えることはないだろう。

~/.profile

各ユーザー個別の設定ファイル。ユーザーがシステムにログインしたときに読み込まれる。

環境変数はここに書くのが良いという意見が多いのだが、個人的には環境変数は~/.bashrcに書いたほうが確実じゃないかと思う。(理由は環境変数は後勝ちで、かつフロー上~/.profileの読み込みの後は~/bashrcが読み込まれるから)

~/.bash_profile

Ubuntu や Raspberry Pi OS では~/.profileが同じ役割を果たすので、使わない。

~/.bashrc

各ユーザー個別の設定ファイルであり、Bash を起動する度に読み込まれる。

エイリアス、関数、プロンプトの設定は~/.bashrcに書く。

~/.bashrc
# エイリアスの設定例
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# 関数の定義例
mkcd() {
    mkdir -p "$1" && cd "$1"
}

# プロンプトの設定例
PS1='[\u@\h \W]\$ '

設定を反映させる

以下のコマンドで編集した~/.bashrcを起動中のシェルに反映させることができる。

$ source ~/.bashrc

参考: 実験

この記事は実験をして書いた。以下は実験を記載している。結果は上にあるとおりなので、別に読まなくても良い。

Raspberry Pi OS の各ファイルの先頭付近に以下を追記する。

etc/profile
echo "/etc/profile opened"
~/.bash_profile
echo "~/.bash_profile opened"
~/.bashrc
echo "~/.bashrc opened"
~/.profile
echo "~/.profile opened"

システムを再起動する。

$ sudo reboot

以下、色々な操作をして出力を確認してみる。

LXTerminal を起動したとき

~/.bashrc opened
$

LXTerminal で新しいタブを起動したとき

~/.bashrc opened
$

シェル上でユーザーを切り替えたとき

$ sudo su -
/etc/profile opened
$ su {ユーザー名} -
~/.bashrc opened
$

bash を起動したとき

$ bash
$ ~/.bashrc opened

外部の端末から SSH 接続したとき

~/.bash_profile opened
$

~/.bash_profile がない状態で外部の端末から SSH 接続したとき

/etc/profile opened
~/.profile opened
~/.bashrc opened
$
2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?