Mac 上將 zsh 用作默認 Shell

從 macOS Catalina 版開始,您的 Mac 將使用 zsh 作為默認登錄 Shell 和交互式 Shell。您還可以在較低版本的 macOS 中將 zsh 設(shè)置為默認 Shell。

默認情況下,您的 Mac 使用 zsh 或 bash 作為登錄 Shell 和交互式 Shell 的命令行解釋器:

  • 從 macOS Catalina 版開始,zsh (Z shell) 是所有新建用戶帳戶的默認 Shell。
  • bashmacOS Mojave 及更低版本中的默認 Shell。

zsh 與 Bourne Shell (sh) 高度兼容,并且與 bash 基本兼容,但存在一些差別。要進一步了解 zsh 及其全面的命令行完成系統(tǒng),請在“終端”中輸入 man zsh。

image

如何更改默認 Shell

無論您的用戶帳戶是配置為使用 zsh(推薦)、bash 還是其他 Shell,您都可以從“用戶與群組”偏好設(shè)置或命令行中更改默認 Shell。

從“用戶與群組”偏好設(shè)置中更改

  1. 選取蘋果菜單 ? >“系統(tǒng)偏好設(shè)置”,然后點按“用戶與群組”。

  2. 點按鎖形圖標(biāo)
    image

    ,然后輸入您的帳戶名稱和密碼。

  3. 在左側(cè)的用戶列表中,按住 Control 鍵點按您的用戶名,然后選取“高級選項”。

  4. 從“登錄 Shell”菜單中選取一個 Shell,然后點按“好”以存儲更改。

image

從命令行中更改

在“終端”中,輸入 $ chsh -s <var style="font-family: &quot;SF Pro SC&quot;, &quot;SF Pro Text&quot;, &quot;SF Pro Icons&quot;, &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-style: normal; color: rgb(102, 102, 102);">path</var>,其中的 <var style="font-family: "SF Pro SC", "SF Pro Text", "SF Pro Icons", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; font-style: normal; color: rgb(102, 102, 102);">path</var> 是 /etc/shells 中列出的某個 Shell 路徑,例如 /bin/zsh、/bin/bash、/bin/csh、/bin/dash、/bin/ksh、/bin/sh 或 /bin/tcsh。

image

如何在不更改默認 Shell 的情況下使用其他 Shell

如果您不希望“終端”在新的“終端”窗口和標(biāo)簽頁中使用默認的登錄 Shell,請按照以下步驟操作:

  1. 打開“終端”,然后選取“終端”>“偏好設(shè)置”。
  2. 從“通用”面板中,選擇“命令(完整的路徑)”。
  3. 在提供的欄位中,輸入 /etc/shells 中列出的某個 Shell 路徑,例如 /bin/zsh、/bin/bash、/bin/csh、/bin/dash、/bin/ksh、/bin/sh 或 /bin/tcsh。
image

如果您調(diào)用 bash Shell,而 macOS Catalina 已配置為使用另一個 Shell,則您將看到一條信息,提示默認的交互式 Shell 現(xiàn)在是 zsh。要消除這個警告,您可以將以下命令添加到 ~/.bash_profile 或 ~/.profile 中:

<pre style="margin: 1em 0px 0px; padding: 8px; font-size: 16px; font-weight: 400; font-style: normal; font-family: "SF Pro SC", "SF Pro Text", "SF Pro Icons", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; border: 1px solid rgb(222, 222, 222); color: rgb(102, 102, 102); background-color: rgb(250, 251, 252); display: block; overflow: auto;">export BASH_SILENCE_DEPRECATION_WARNING=1
</pre>

image

如何切換到 zsh 描述文件和提示符

如果您正在使用 bash 描述文件(例如,為了設(shè)置環(huán)境變量、別名或路徑變量),則您應(yīng)改為使用作用與它相同的 zsh 描述文件。例如:

  • .zprofile 的作用與 .bash_profile 相同,并且在登錄時運行(包括通過 SSH 運行)
  • .zshrc 的作用與 .bashrc 相同,并針對每個新的“終端”會話運行

如果您正在使用 .profile(一個兼容 POSIX 的描述文件),則您可以將以下命令添加到 .zprofile,以便 zsh 自動讀取這個描述文件的設(shè)置:

<pre style="margin: 1em 0px 0px; padding: 8px; font-size: 16px; font-weight: 400; font-style: normal; font-family: "SF Pro SC", "SF Pro Text", "SF Pro Icons", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; border: 1px solid rgb(222, 222, 222); color: rgb(102, 102, 102); background-color: rgb(250, 251, 252); display: block; overflow: auto;">[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'
</pre>

另外,您無需進行任何修改,就可以將 bash 描述文件中的某些設(shè)置轉(zhuǎn)移到 zsh 描述文件。例如,要設(shè)置環(huán)境變量,您可以使用以下命令:export MY_SETTING=1。

zsh 能夠識別一組不同于 bash 所能識別的提示符指定符,并且用于指定彩色輸出的語法更簡潔,讓您無需使用復(fù)雜的 ANSI 轉(zhuǎn)義序列。例如,下面顯示了 .bash_profile 中默認 bash 提示符的語法:

<pre style="margin: 1em 0px 0px; padding: 8px; font-size: 16px; font-weight: 400; font-style: normal; font-family: "SF Pro SC", "SF Pro Text", "SF Pro Icons", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; border: 1px solid rgb(222, 222, 222); color: rgb(102, 102, 102); background-color: rgb(250, 251, 252); display: block; overflow: auto;">export PS1="[\e[92;40m]\h[\e[m]:[\e[93m]\W[\e[m] \$ "
</pre>

要在使用 .zprofile.zshrc 時將這個 prompt 提示符轉(zhuǎn)換為 zsh 提示符,請使用以下命令:

<pre style="margin: 1em 0px 0px; padding: 8px; font-size: 16px; font-weight: 400; font-style: normal; font-family: "SF Pro SC", "SF Pro Text", "SF Pro Icons", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; border: 1px solid rgb(222, 222, 222); color: rgb(102, 102, 102); background-color: rgb(250, 251, 252); display: block; overflow: auto;">export PS1="%10F%m%f:%11F%1~%f $ "
</pre>

請參閱 zsh man 頁面,以了解更多詳細信息。

image

如何測試 Shell 腳本

要在 macOS Catalina 中使用與 Bourne 兼容的 Shell 來測試腳本兼容性,您可以將 /var/select/sh 更改為 /bin/bash、/bin/dash 或 /bin/zsh。如果您將 /var/select/sh 更改為 bash 之外的 Shell,請注意,使用 bashisms 的腳本可能無法正常工作。

您可以運行命令 zsh --emulate sh 來讓 zsh 模擬 sh。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容