用戶家目錄下.bash_profile 與 .bashrc 的區別


【.bash_profile 與 .bashrc 的區別】
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

【login shell 與 non-login shell 的區別】
1、當你直接在機器login界面登陸、使用ssh登陸或者su切換用戶登陸時,.bash_profile 會被調用來初始化shell環境
Note:.bash_profile文件默認調用.bashrc文件
.bash_profile中有如下內容
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi 
2、當你不登陸系統而使用ssh直接在遠端執行命令,.bashrc 會被調用
3、當你已經登陸系統後,每打開一個新的Terminal時,.bashrc 都會被再次調用。

測試準備工作
hclient2主機hadoop用戶家目錄下執行
[hadoop@hclient2 ~]$ echo "invoke hclient2:~/.bashrc">>.bashrc
[hadoop@hclient2 ~]$ echo "invoke hclient2:~/.bash_profile">>.bash_profile

Login Shell
1、窗口登陸
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel 2.6.32-279.el6.x86_64 on an x86_64

hclient2 login: hadoop
Password:
Last login: Mon Feb 25 23:03:45 on tty1
invoke hclient2:~/.bashrc
invoke hclient2:~/.bash_profile
[hadoop@hclient2 ~]$
2、SSH 登陸
[hadoop@hserver ~]$ ssh hclient2
Last login: Mon Feb 25 22:42:19 2013 from hserver
invoke hclient2:~/.bashrc
invoke hclient2:~/.bash_profile
[hadoop@hclient2 ~]$
3、su 登陸
[root@hclient2 ~]# su - hadoop
invoke hclient2:~/.bashrc
invoke hclient2:~/.bash_profile

Non-login Shell:
Note: ssh ...[user@] hostname [command]
If command is specified, it is executed on the remote host instead of a login shell.
[hadoop@hserver ~]$ ssh hclient2 hostname
invoke hclient2:~/.bashrc
hclient2


【故若要配置環境變量之類,最保險是寫在 .bashrc 文件中。因爲不管是登陸還是不登陸,該文件總會被調用!





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章