bash_profile和bashrc區別

交互式模式就是shell等待你的輸入,而且執行你提交的命令。非交互式模式,在這種模式下,shell不與你進行交互,而是讀取存放在文件中的命令,而且執行它們。shell

當第一個用戶登陸時(login交互式shell),執行/etc/profile和~/.bash_profile;當運行bash shell時(nologin交互式shell),執行~/.bashrc。/etc/profile調用/etc/profile.d目錄下的配置文件;~/.bash_profile會調用~/.bashrc;~/.bashrc會調用/etc/bashrc。bash

~/.bash_logout登出時執行一些清理任務ssh

在當前shell定義的alias只在當前shell生效ide

因此若是要全局聲明別名或者變量的話:變量寫在/etc/profile.d/目錄下,別名寫在/etc/bashrc下oop


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


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


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


Login Shell
一、窗口登錄
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 ~]$
二、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 ~]$
三、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 文件中。由於無論是登錄仍是不登錄,該文件總會被調用!
測試

相關文章
相關標籤/搜索