2008.09.14 Sunday
(dot).bashrc .bash_profile .profile の読み込み順
.bashrc、.bash_profile 、.bash_login、.profileの読まれるタイミングの理解が不足していたので検証してみました通常のログイン(login) &
su -
.bash_profile 無い場合は.bash_login→.profile
スクリプト中から.bashrcを呼び出しています
su
.bashrc のみが呼ばれる
/bin/bash
.bashrc が呼ばれますが、/bin/sh に /bin/bashがリンクしてある場合、 /bin/sh として呼ばれた場合は.bashrcは呼ばれません
やはり.bashrcに個人設定を書き、.profile系から呼び出すのが基本なようです
.profile系が無いシステムでは、.ログイン時は.bashrcは実行されません、あまり意識していませんでしたがログインシェルを持つユーザにとって.bash_profile( or .bash_login or .profile)は必須だということになります
又、.profileに環境変数を追加しても、前段の.bash_profileファイルが存在する場合は無視され、環境変数は反映されない事になります
それと、.bashrcが呼ばれるタイミングは最後では無いので注意が必要です
「続き」に検証結果を載せておきます
リンク:
・@IT 第9回 bashの便利な機能を使いこなそう
・manpage su
・manpage login
・manpage bash
準備:
.bashrcには "I am bashrc"
.bash_profileには"I am bash_profile"
.profile に "I am profile."
を出力するようにしてテスト
.bashrc_profileから.bashrcを呼ぶ部分はコメントアウト
.bashrcと.bash_profileは選択的に実行される為、適切に.bashrcを実行するには.bash_profileに.bashrcを呼び出す必要があります
ログイン時処は.bash_profile で、その後のユーザ設定等はは.bashrcに書くのがいいかと思います。
・bash_loginは.profileの前に呼ばれます
.bashrcには "I am bashrc"
.bash_profileには"I am bash_profile"
.profile に "I am profile."
を出力するようにしてテスト
.bashrc_profileから.bashrcを呼ぶ部分はコメントアウト
Linux eva 2.6.18-6-amd64 #1 SMP Mon Jun 16 22:30:01 UTC 2008 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Sep 13 21:18:53 2008 from 192.168.1.144
I am bash_profile -->通常ログインでは .bash_profile
test@eva:~$ su test -->通常のシェル起動では
Password:
I am bashrc. -->.bashrcのみ
test@eva:~$ su - test -->環境変数適用させtestでログイン
Password:
I am bash_profile -->.bash_profile が読まれる
test@eva:~$ /bin/bash -->bashバイナリ起動では
I am bashrc. -->.bashrが読まれ
test@eva:~$ sh -->リンク経由(bash)では
sh-3.1$ -->なにも呼ばれません
test@eva:~$ ls -l `which sh`
lrwxrwxrwx 1 root root 4 2008-09-12 02:04 /bin/sh -> bash
test@eva:~$ mv .bash_profile .bash_profile.org
-->.bash_profileを消去してみると
test@eva:~$ su - test
Password:
I am profile. -->.profileが呼ばれました
test@eva:~$ mv .bashrc .bashrc.org
-->.bashrcを消去してみると
est@eva:~$ su test
Password:
test@eva:~$ -->なにも呼ばれません
test@eva:~$ bash --version
bash --version
GNU bash, version 3.1.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
.bashrcと.bash_profileは選択的に実行される為、適切に.bashrcを実行するには.bash_profileに.bashrcを呼び出す必要があります
ログイン時処は.bash_profile で、その後のユーザ設定等はは.bashrcに書くのがいいかと思います。
・bash_loginは.profileの前に呼ばれます