Zsh 沒有點擊 ~/.profile

Zsh 沒有點擊 ~/.profile

我剛剛在 Ubuntu 系統上安裝了 zsh。看來 zsh 並沒有在 init 上執行 ~/.profile 。據我所知,這應該是一種自動行為。我缺什麼?

答案1

.profile.zprofile

當 Zsh 作為登入 shell 呼叫時,它會運行~/.zprofile,而不是。~/.profile原因是 zsh 與標準 shell 的不相容性足以破壞腳本。

~/.profile如果 Zsh 被稱為sh或,它就會運作ksh。但如果您的目標是在登入時獲得 zsh 提示,那就沒有幫助了。

您可以製作/bin/sh登入 shell 並將其包含export SHELL=/bin/zsh在您的~/.profile.然後,當您打開終端時,終端將啟動 zsh(除了少數終端模擬器不遵守該$SHELL設定)。但當sh您透過 ssh 登入時,您仍然可以使用。這可以透過exec zsh在末尾包含來解決~/.profile(這會用 zsh 替換正在運行的 shell),但是您需要小心,僅在互動式登入時執行此操作,而不是在~/.profile從其他腳本(例如X 會話啟動)中包含時執行此操作(一個很好的方法) test 是透過以下方式獲得的父進程的名稱ps -o comm= $PPID:如果是sshdsu,則可以安全exec)。

使用 zsh 和 run 的最簡單解決方案~/.profile是創建一個~/.zprofile在運行時進入 sh 模擬模式的解決方案~/.profile

emulate sh
. ~/.profile
emulate zsh

如果你有一個足夠新的 zsh (在 Ubuntu 上,我認為這意味著自從 lucid 以來),你可以將其簡化為emulate sh -c '. ~/.profile'.

.zprofile.zshrc

該文件~/.profile由以下方式加載登入貝殼。登入 shell 是您以文字模式(例如在文字控制台或透過 ssh)登入時啟動的第一個進程。預設情況下,在大多數 Linux 機器上,登入 shell 是 bash,但您可以使用命令chsh或透過其他工具(例如 Ubuntu 中的「使用者設定」)來變更它。當它是登入 shell 時,bash 會讀取~/.bash_profile它是否存在 和~/.profile,而 zsh 只會讀取~/.zprofile(因為它的語法與傳統 sh 不完全相容)。在大多數配置下,~/.profile當您登入圖形顯示管理器時,X 會話啟動腳本也會載入。

當您啟動終端模擬器並取得 shell 提示字元時,或明確啟動 shell 時,您將獲得一個不是登入 shell 的 shell。由於~/.profile(或~/.zprofile) 用於登入時要執行的命令,因此非登入 shell 不會讀取此檔案。相反,當您啟動互動式 zsh 時,它會顯示~/.zshrc. (Zsh 讀取~/.zshrc所有互動式 shell,無論它們是登入 shell 都不是;奇怪的是,bash 從不讀取~/.bashrc登入 shell。)

通常,~/.profile包含環境變數定義,並且可能會啟動一些您希望在登入時或整個會話中執行一次的程式;~/.zshrc包含每個 shell 實例必須完成的事情,例如別名和函數定義、shell 選項設定、完成設定、提示設定、鍵綁定等。

答案2

對於不耐煩的人的簡短回答:

  1. ~/.profilezsh登入時未載入。
  2. zsh~/.zprofile登入時載入。
  3. zsh~/.zshrc啟動新的終端機會話時載入。

需要更多資訊嗎?看看吉爾斯的精彩回答!

答案3

除了 Gilles 的回答之外,使用 zsh 的最新版本,您還可以執行以下操作:

[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'

……這將在 zsh 的 sh 模式生效的情況下取得 .profile 檔案。並且它僅在來源期間有效。因此,您無需儲存當前選項狀態即可在採購後再次重播。

答案4

桀騁我手頭上的文檔僅提到了/etc/profile登入~/.profileshell/克什相容模式。

% zsh --version
zsh 4.3.10 …
% cat ~/.profile
echo 'Running ~/.profile...'

本機模式登入 shell(argv[0] 以 開頭-)不使用~/.profile(但它會使用~/.zprofile):

% zsh -c 'exec -a -zsh zsh' </dev/null

(無輸出)

/克什相容模式登入 shell 使用 .profile:

% zsh -c 'exec -a -sh zsh' </dev/null
Running ~/.profile...
% zsh -c 'exec -a -ksh zsh' </dev/null
Running ~/.profile...

相關內容