2017-01-14 22 views
1

我使用的是Ubuntu 16.04。在我的$HOME/.profile,它具有.profile有聲明調用.bashrc,但.bashrc不執行

echo `date` ':.profile executed starts' >> /tmp/testtmp.txt 
echo $BASH_VERSION >> /tmp/testtmp.txt 
if [ -n "$BASH_VERSION" ]; then 
echo 'if 1 entered' >> /tmp/testtmp.txt 
    # include .bashrc if it exists 
    if [ -f "$HOME/.bashrc" ]; then 
    echo 'if 2 entered' >> /tmp/testtmp.txt 
    . "$HOME/.bashrc" 
    fi 
else 
    echo 'if 1 not entered' >> /tmp/testtmp.txt 
fi 

我每次登錄,它甚至從來沒有達到第一IF

測試文件顯示:

Sat Jan 14 05:15:57 EST 2017 :.profile executed starts 

if 1 not entered 
Sat Jan 14 05:15:57 EST 2017 :.profile executed ends 

我贊同$ BASH_VERSION,它顯示4.3.46(1)-release。所以如果if [ -n "$BASH_VERSION" ]是爲了檢查變量是否存在,(顯然是),爲什麼它甚至沒有達到第一個IF?

從結果看來,直到我第一次調用終端應用程序時,纔會生成$ BASH_VERSION。

由於.profile中的那些IF語句是原創的,我不明白如果在登錄時沒有生成$ BASH_VERSION,那麼最初擁有這些語句的目的是什麼。

感謝

+0

你怎麼知道它永遠不會到達第一個'if'聲明?另外,是不是正在執行的'.profile'或'.bashrc'?你的頭銜說一個,但身體說另一個。 –

+0

你是怎麼稱呼.profile文件的?使用'source〜/ .profile'?還有,你在哪裏回覆了$ BASH_VERSION –

+0

抱歉的傢伙。我更新了我的問題。 –

回答

0

(這是越來越過分,只要評論:))

參見向Ubuntu的this question.profile可能運行在sh之下,而不是bash,所以BASH_VERSION未在.profile中設置。

爲了驗證這一點,添加

ps >> /tmp/testtmp.txt 

.profile。你會看到你正在運行的shell的進程名,這可能是this

要查看您的默認外殼是什麼,grep <whatever your username is> /etc/passwd - 您將看到/bin/<whatever>sh

如果確實/bin/bash未設置爲默認的shell,你可以改變它(每this answer):

sudo chsh -s /bin/bash <whatever your username is> 
+0

謝謝。這是我的測試結果。 1.'/ etc/passwd'中的我的用戶標識符表示它使用'/ bin/bash' 2.'ps >> /tmp/testtmp.txt'告訴我什麼都沒有與sh或bash相關。 –

+0

我在askubuntu上看過那篇文章。在他的情況下,通過從sh到bash的chsh登錄shell,它解決了他的問題。但是由於我的登錄shell已經是bash,我不知道我還能做什麼。 –

+0

我硬編碼運行.bashrc到.profile中。與askubuntu中的帖子相同。它無法解析.bashrc中的bash語句。 –