2015-04-15

Ubuntu 讀取 .bashrc 和 .bash_profile 檔案的時機

由於阿舍開始大量使用 alias 的時機,是在經常使用沒有圖型介面的 Ubuntu Server 之後開始的,因此,阿舍都是用 SSH 連進去的,所以,阿舍就理所當然的認為,一些常用的 alias 要寫在 .bash_profile 這個檔案裡才會有作用的,然後,當阿舍回到圖型介面的終端機之後,就發現,事情好像不是這樣子的哩 !



原來,這個 .bash_profile 是要透過登入的方式進入 Shell 時,系統才會去讀 .bash_profile 這個檔案,而像阿舍是進 X Window 後再打開終端機來進入 Shell 的話,那就不會去讀 .bash_profile 這個檔,而是會去讀 .bashrc 這個檔案,所以,阿舍設定在 .bash_profile 的 alias 和 function 就沒有用了哩 !  

 手動執行 bash 也會載入 .bashrc 

後來,阿舍再查了一下才知道,這 .bash_profile 和 .bashrc 兩個檔的讀取時機是以 Login Shell 和 Interactive Shell 來區分的,簡單說,就是登入時啟動的是會自動載 .bash_profile,而登入之後才啟動 Shell 的話,就會去讀 .bashrc 這個檔,所以,如果你手動執行執行 bash 指令的話,也是會去載入 .bashrc 的哩 !

那如果有用圖型介面的話,那不就每次都要去設定兩個檔嗎 ? 嗯 ... 阿舍一開始也是這樣想的,不過,後來想一想,阿舍怎麼那單純哩 ! 就都設定在 .bashrc 檔,然後,在 .bash_profile 加入下面這一行來把 .bashrc 檔給載入不就好了嗎 .....

. ~/.bashrc

or

source ~/.bashrc


推薦閱讀


有疑問? 問題還是沒解決嗎? 歡迎下方留言提問和討論 😁

7 則留言 :

Unknown 提到...

$ cat ~/.profile

可以看到上方的註解「This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
exists.」

加上下方還有一段程式碼-「(# if running bash)以下」。

我測試的結果,若「~/.bash_profile」不存在,就都會執行「.bashrc」。



# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi

Arthur 提到...

Hello,

謝謝提供資訊息。
不過,阿舍這篇主要寫的是在 X Window 裡開終端機的情況下, ~/.profile 似乎只會在登入時讀取,另外再開終端機就不會再讀了哩 !...

供參囉 ! ...

Unknown 提到...


我要表達的是

```
我測試的結果,若「~/.bash_profile」不存在,就都會執行「.bashrc」。
```

所以當你把「~/.bash_profile」拿掉。

* 當「登入」的時候,會直接執行「~/.profile」,
然後「~/.profile」裡面的程式碼,會執行「~/.bashrc」。
,所以「登入」的時候,是會執行「.bashrc」。

* 當在「X Window 開 Terminal」的時候,不會執行「~/.profile」,但會執行「~/.bashrc」。

以上的前提,「預設的shell」是使用「bash」。

所以,若「~/.bash_profile」不存在,就都會執行「.bashrc」。


====================================================


而你的方式,因為「~/.bash_profile」存在。

* 「登入的時候」,執行「~/.bash_profile」,然後你在「~/.bash_profile」,再加入執行「. ~/.bashrc」。

* 在「X Window 開 Terminal」的時候,執行「.bashrc」。


所以你的方式,其實是一樣的。
只不過「~/.profile」換成「~/.bash_profile」。


附註: 可以在這些script,加入「echo」訊息,測試的時候,就可以看到執行順序了。

====================================================

所以有一種方式, 可以把「.bash_profile」拿掉。
然後自己寫的script,寫在另一個檔,另外命名,不要叫「.bash_profile」。
然後在「.bashrc」去執行。

另外你也可以仿造「/etc/profile」,
裡面「if [ -d /etc/profile.d ]; then」哪一段,
寫在「~/.bashrc」裡。
就會自動執行某個資料夾裡的script檔案。


以上參考
====================================================



:-)






Unknown 提到...

我要表達的是

```
我測試的結果,若「~/.bash_profile」不存在,就都會執行「.bashrc」。
```

所以當你把「~/.bash_profile」拿掉。

* 當「登入」的時候,會直接執行「~/.profile」,
然後「~/.profile」裡面的程式碼,會執行「~/.bashrc」。
,所以「登入」的時候,是會執行「.bashrc」。

* 當在「X Window 開 Terminal」的時候,不會執行「~/.profile」,但會執行「~/.bashrc」。

以上的前提,「預設的shell」是使用「bash」。

所以,若「~/.bash_profile」不存在,就都會執行「.bashrc」。

Unknown 提到...

而你的方式,因為「~/.bash_profile」存在。

* 「登入的時候」,執行「~/.bash_profile」,然後你在「~/.bash_profile」,再加入執行「. ~/.bashrc」。

* 在「X Window 開 Terminal」的時候,執行「.bashrc」。


所以你的方式,其實是一樣的。
只不過「~/.profile」換成「~/.bash_profile」。


附註: 可以在這些script,加入「echo」訊息,測試的時候,就可以看到執行順序了。

Unknown 提到...

所以有一種方式, 可以把「.bash_profile」拿掉。
然後自己寫的script,寫在另一個檔,另外命名,不要叫「.bash_profile」。
然後在「.bashrc」去執行。

另外你也可以仿造「/etc/profile」,
裡面「if [ -d /etc/profile.d ]; then」哪一段,
寫在「~/.bashrc」裡。
就會自動執行某個資料夾裡的script檔案。


以上參考

Arthur 提到...

Hello,

瞭解。 謝謝囉 ! ... ^^=

張貼留言

歡迎留言提問和討論 .... 😁