[英]How to load ~/.bash_profile when entering bash from within zsh?
我已經使用 bash 兩年了,只是嘗試通過自制軟件在我的 OS X 上切換到 zsh shell。 我將我的默認(登錄)shell 設置為 zsh,並確認它設置正確,因為當我啟動終端時,它是默認使用的 zsh shell。
但是,當我嘗試從 zsh 中進入 bash shell 時,它似乎沒有加載
~\/.bash_profile<\/code> ,因為我無法使用別名運行我的命令,別名是在我的
~\/.bash_profile<\/code>定義的,例如
alias julia="~\/juila\/julia"<\/code>等。此外,提示不是我在文件中設置的,而是返回
bash-3.2$<\/code> 。
由於某些原因,當我將登錄 shell 設置為 bash 並從 bash 中輸入 zsh 時,
~\/.zshrc<\/code>會正確加載。
那么為什么當我從 zsh 中運行
bash<\/code>時它沒有加載呢?
我的
~\/.bash_profile<\/code>符號鏈接到
~\/Dropbox\/.bash_profile<\/code>以便與我的其他計算機同步。
也許它會導致問題?
打開~/.zshrc
,並在文件的最底部,添加以下內容:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile;
fi
每次打開終端時,它都會加載~/.bash_profile
定義的任何內容(如果文件存在)。 這樣,您可以保留zsh的自定義設置(顏色等)。 您可以將自定義 shell 設置保存在.bash_profile
文件中。
這比使用bash -l
IMO 干凈得多。
如果您更喜歡將設置放在.bashrc
、 .bash_login
或.profile
,您可以對它們執行相同的操作。
交互式bash
讀取您的~/.bash_profile
如果它是登錄 shell,或者您的~/.bashrc
如果它不是登錄 shell。
典型的.bash_profile
將包含以下內容:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
因此.bashrc
可以包含要由登錄或非登錄 shell 執行的命令。
如果您運行bash -l
而不僅僅是bash
,它應該讀取您的.bash_profile
。
參考: https : //www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
對於那些剛剛安裝 zsh 並希望他們的 bash 別名在 zsh 上工作的人,請執行以下操作
像這樣在vim中打開.zshrc文件
vi ~/.zshrc
滾動到底部
source ~/.bash_profile
:wq
source ~/.zshrc
就是這樣。 現在,您在 .bash_profile 中保存的所有別名都可以在 zsh 中使用了。為了補充@Keith Thompson 的出色回答:
系統:
正如@chepner 簡潔地說的那樣(強調我的):
在 OS X 中,bash 不用作初始 [在啟動時] 登錄過程的一部分,並且 Terminal.app(或其他終端模擬器)進程存在於任何預先存在的 bash 會話之外,因此每個新窗口 [或選項卡 -閱讀:交互式 bash shell](默認情況下)將自己視為新的登錄會話。
因此,一些 OSX 用戶只創建~/.bash_profile
,而從不打擾~/.bashrc
,因為所有交互式 bash shell 都是登錄shell。
Linux :
在 Linux 上,情況通常是相反的:交互創建的bash
shell 是 [交互式]非登錄shell,所以重要的是~/.bashrc
。
因此,許多 Linux 用戶只處理~/.bashrc
。
要維護適用於兩個平台的 bash 配置文件,請使用@Keith Thompson 提到的技術:
~/.bashrc
~/.bash_profile
下行添加到~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
從 ~/.bash_profile 復制內容並將它們粘貼到 ~/.zshrc 文件的底部。
對於 MacOs 上的 ZSH 用戶,我最終得到了一個襯墊。
在~/.zshrc 的最底部,我添加了以下行:
bash -l
它所做的只是加載.bash_profile設置(別名、函數、導出 $PATH、...)
如果您決定擺脫 ZSH 並返回到普通的 BASH,您將完全恢復正常。
如果這是您很少做的事情,或者不適合進行更改,您還可以在啟動子 bash shell 后“獲取” .bash_profile 。
. ~/.bash_profile
這將在該 shell 會話的生命周期中引入您在.bash_profile腳本中所做的設置。 在大多數情況下,您應該能夠重復該命令,因此這也是一種無需完全登錄即可測試您所做的任何更改的簡單方法,並且如果出現以下情況,則可以使所有現有的 shell 會話保持最新狀態您對.bash_profile和/或.bashrc文件進行升級。
適用於 macOS Big Sur(版本 11.5.2)
打開
.zshrc<\/code>
例如: sudo nano ~\/.zshrc<\/code>
<\/li><\/ul><\/li>
在文件末尾添加
source ~\/.bash_profile<\/code>
每次打開終端時,都會加載 bash 配置文件中的內容。
我正在使用一個名為oh my zsh的 zsh 框架,我已經嘗試了此處列出的大多數解決方案,但它破壞了我的自定義主題的格式。 但是,這些步驟對我有用。
在我的.bash_profile底部添加新別名
vi ~/.bash_profile
讓zsh從.bash_profile加載項目
source ~/.bash_profile
刷新zsh
source ~/.zshrc
重新啟動 OSX 終端應用程序
試試你的新別名!
如果您想“以個人資料為中心”,您可以創建.profile
作為單一事實來源,然后從.bash_profile
和.zprofile
加載它。
。輪廓
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# etc., etc.
.bash_profile和.zprofile
if [ -f ~/.profile ]; then
. ~/.profile;
fi
我發現這有助於 bash 腳本找到正確的PATH
等,並幫助我將配置保留在一個地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.