Mac 終端機 Shell 完全指南:Bash vs Zsh

深入了解 macOS 上的 Shell 環境:Bash 與 Zsh 的差異、配置與最佳實踐


目錄


什麼是 Shell?

核心概念

  • Shell 是一個命令列介面程式,讓你透過文字命令與作業系統互動
  • 就像是「人類語言」和「電腦語言」之間的翻譯員
  • 當你在終端機輸入命令時,實際上是在與 shell 對話

Shell 的角色

你 → 輸入命令 → Shell 解析 → 作業系統執行 → Shell 顯示結果 → 你看到輸出

Mac 上的 Shell 演進史

時間軸

時期 預設 Shell 版本 說明
2001-2019 Bash 3.2 macOS 長期預設,穩定但舊版
2019-現在 Zsh 5.x+ macOS Catalina 起的新預設

為什麼 Apple 從 Bash 換成 Zsh?

  1. 授權問題

    • Bash 3.2 是 2007 年的版本(GPL v2 授權)
    • Bash 4.0+ 改用 GPL v3 授權,Apple 不願採用
    • Zsh 使用 MIT-like 授權,對 Apple 更友善
  2. 功能更強

    • Zsh 內建更多現代化功能
    • 更好的自動補全
    • 更強的主題和插件系統

常見的 Shell 種類

1. Bash (Bourne Again Shell)

# 特點
- 最普及的 Shell(Linux 預設)
- 語法簡單,學習資源豐富
- macOS 內建版本較舊(3.2,2007年)
- 腳本相容性最好

# 查看版本
bash --version
# 輸出:GNU bash, version 3.2.57(1)-release

優點

  • ✅ 跨平台相容性最好(幾乎所有 Unix 系統都有)
  • ✅ 大量教學資源和腳本範例
  • ✅ 穩定可靠

缺點

  • ❌ macOS 版本太舊(卡在 3.2)
  • ❌ 預設功能較少(需要手動配置)
  • ❌ 自動補全功能較弱

2. Zsh (Z Shell)

# 特點
- macOS Catalina (2019) 起的預設 Shell
- 功能強大,高度可自訂
- 相容大部分 Bash 語法
- 支援 Oh My Zsh 框架

# 查看版本
zsh --version
# 輸出:zsh 5.9

優點

  • ✅ macOS 原生預設(2019+)
  • ✅ 強大的自動補全(Tab 補全更智慧)
  • ✅ 內建拼寫檢查和錯誤建議
  • ✅ 豐富的主題和插件生態(Oh My Zsh)
  • ✅ 更好的陣列和字串處理

缺點

  • ❌ 某些 Bash 腳本可能不相容
  • ❌ 學習曲線稍陡(功能太多)
  • ❌ 配置文件不同(.zshrc vs .bashrc

3. 其他 Shell(了解即可)

Shell 說明 適用情境
sh 最基礎的 Shell,語法簡單 寫跨平台腳本
csh/tcsh C 語言風格語法 舊系統,少用
fish 現代化、開箱即用 重視使用者體驗
dash 輕量、快速 系統腳本

如何查看和切換 Shell

查看當前使用的 Shell

# 方法 1:查看環境變數
echo $SHELL
# 輸出:/bin/zsh (你的預設 shell)

# 方法 2:查看當前 Shell
echo $0
# 輸出:-zsh 或 -bash

# 方法 3:查看進程
ps -p $$
# 輸出:
# PID TTY      TIME CMD
# 1234 ttys000  0:00.10 -zsh

查看系統可用的 Shell

cat /etc/shells

輸出範例

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

暫時切換 Shell

# 切換到 bash(當前終端有效)
bash

# 切換到 zsh
zsh

# 退出回到原本的 shell
exit

永久更換預設 Shell

方法 1:使用 chsh 命令(推薦)

# 查看當前預設 Shell
echo $SHELL

# 更改為 zsh
chsh -s /bin/zsh

# 更改為 bash
chsh -s /bin/bash

# ⚠️ 需要重新登入或重開終端機才會生效

方法 2:透過系統設定

  1. 開啟「系統設定」→「使用者與群組」
  2. 解鎖(點擊左下角鎖頭)
  3. 右鍵點擊你的使用者 → 「進階選項」
  4. 在「登入 Shell」欄位選擇 Shell

Bash vs Zsh 功能對照

1. 自動補全

Bash 的補全

# 按 Tab 一次:如果唯一則補全,否則無反應
# 按 Tab 兩次:列出所有可能選項

cd Do[Tab][Tab]
# 顯示:Documents/ Downloads/

Zsh 的補全(更智慧)

# 按 Tab 一次:列出選項並可以用方向鍵選擇
cd Do[Tab]
# 自動顯示:
# Documents/  Downloads/
# 可以用 ↑↓ 選擇

# 還支援中途補全
cd D/cu[Tab]
# 自動補全成:cd Documents/current/

2. 拼寫檢查

Bash

# 拼錯不會提示
cd Docuemnts
# bash: cd: Docuemnts: No such file or directory

Zsh

# 會提示並詢問是否修正
cd Docuemnts
# zsh: correct 'Docuemnts' to 'Documents' [nyae]?
# n = no, y = yes, a = abort, e = edit

3. 萬用字元 (Globbing)

Bash

# 基本萬用字元
ls *.txt          # 所有 .txt 檔案
ls file[1-3].txt  # file1.txt, file2.txt, file3.txt

Zsh(更強大)

# 遞迴搜尋
ls **/*.txt       # 所有子目錄的 .txt 檔案

# 排除特定檔案
ls *~*.log        # 所有檔案,但排除 .log

# 限定類型
ls *(.)           # 只顯示檔案(不含目錄)
ls *(/)           # 只顯示目錄

# 依修改時間
ls *(m-1)         # 最近 1 天內修改的檔案

4. 陣列操作

Bash

# 陣列從 0 開始
arr=(a b c)
echo ${arr[0]}    # 輸出:a
echo ${arr[@]}    # 輸出:a b c

Zsh

# 陣列從 1 開始(!)
arr=(a b c)
echo $arr[1]      # 輸出:a
echo $arr         # 輸出:a b c(整個陣列)

配置文件差異

Bash 配置文件

檔案 用途 載入時機
~/.bash_profile 登入時執行 開啟終端機
~/.bashrc 互動式 shell 每次新 bash session
~/.bash_logout 登出時執行 關閉終端機

Bash 載入順序

登入 → .bash_profile → .bashrc

常見配置範例 (~/.bashrc):

# 別名
alias ll='ls -lah'
alias gs='git status'

# 環境變數
export PATH="$HOME/bin:$PATH"
export EDITOR=vim

# 提示符
export PS1='\u@\h:\w\$ '

Zsh 配置文件

檔案 用途 載入時機
~/.zshenv 環境變數 每次啟動(包含腳本)
~/.zprofile 登入時執行 開啟終端機
~/.zshrc 互動式配置 每次新 zsh session
~/.zlogin 登入後執行 開啟終端機後
~/.zlogout 登出時執行 關閉終端機

Zsh 載入順序

登入 → .zshenv → .zprofile → .zshrc → .zlogin

常見配置範例 (~/.zshrc):

# 啟用自動補全
autoload -Uz compinit
compinit

# 別名
alias ll='ls -lah'
alias gs='git status'

# 環境變數
export PATH="$HOME/bin:$PATH"
export EDITOR=vim

# 提示符主題
autoload -Uz promptinit
promptinit
prompt adam1

# 歷史紀錄設定
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history

# 自動修正拼寫
setopt correct

# 共享歷史紀錄
setopt share_history

Oh My Zsh:讓 Zsh 更強大

什麼是 Oh My Zsh?

  • Zsh 的配置管理框架
  • 提供 300+ 插件和 150+ 主題
  • 讓 Zsh 配置變得超級簡單

安裝 Oh My Zsh

# 使用 curl 安裝
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 或使用 wget
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

常用 Oh My Zsh 插件

編輯 ~/.zshrc

# 啟用插件
plugins=(
    git              # Git 別名和自動補全
    zsh-autosuggestions  # 根據歷史建議命令
    zsh-syntax-highlighting  # 語法高亮
    docker           # Docker 自動補全
    kubectl          # Kubernetes 自動補全
    npm              # npm 自動補全
    golang           # Go 相關別名
)

安裝第三方插件

# zsh-autosuggestions(命令建議)
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# zsh-syntax-highlighting(語法高亮)
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

更換 Oh My Zsh 主題

# 編輯 ~/.zshrc
vim ~/.zshrc

# 找到 ZSH_THEME,修改為:
ZSH_THEME="robbyrussell"   # 預設主題
# 或
ZSH_THEME="agnoster"       # 人氣主題
ZSH_THEME="powerlevel10k/powerlevel10k"  # 最強主題

# 儲存後重新載入
source ~/.zshrc

推薦主題

  • robbyrussell:簡潔預設
  • agnoster:顯示 Git 狀態
  • powerlevel10k:高度客製化(需另外安裝)

實用技巧

1. 讓 Bash 腳本在 Zsh 中執行

方法 1:在腳本開頭指定 Shell

#!/bin/bash
# 這個腳本會用 bash 執行,即使你用的是 zsh

echo "This runs in bash"

方法 2:執行時指定 Shell

# 用 bash 執行
bash script.sh

# 用 zsh 執行
zsh script.sh

2. 在 Zsh 中臨時切換到 Bash

# 進入 bash
bash

# 檢查
echo $SHELL  # 仍顯示 /bin/zsh(預設沒變)
echo $0      # 顯示 bash(當前 shell)

# 退出回到 zsh
exit

3. 同時維護 Bash 和 Zsh 配置

建立共用的配置文件:

# 建立 ~/.shell_common
vim ~/.shell_common

內容 (~/.shell_common):

# 共用的別名和環境變數
alias ll='ls -lah'
alias gs='git status'
export PATH="$HOME/bin:$PATH"
export EDITOR=vim

在 Bash 中載入 (~/.bashrc):

# 載入共用配置
if [ -f ~/.shell_common ]; then
    source ~/.shell_common
fi

在 Zsh 中載入 (~/.zshrc):

# 載入共用配置
if [ -f ~/.shell_common ]; then
    source ~/.shell_common
fi

4. 常用別名(Alias)

# 加到 ~/.zshrc 或 ~/.bashrc

# 目錄導航
alias ..='cd ..'
alias ...='cd ../..'
alias ~='cd ~'

# 列出檔案
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'

# Git 快捷
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'

# 安全操作
alias rm='rm -i'   # 刪除前確認
alias cp='cp -i'   # 覆蓋前確認
alias mv='mv -i'   # 移動前確認

# 其他
alias grep='grep --color=auto'
alias h='history'
alias c='clear'

常見問題與解決

Q1: 為什麼我的腳本在 Zsh 不能執行?

原因:Bash 和 Zsh 語法略有差異

解決

# 在腳本開頭明確指定 shell
#!/bin/bash

# 或用 bash 執行
bash your_script.sh

Q2: 如何知道某個命令是 Shell 內建還是外部程式?

# 使用 type 命令
type cd
# 輸出:cd is a shell builtin

type ls
# 輸出:ls is /bin/ls

type git
# 輸出:git is /usr/bin/git

Q3: 為什麼修改 .zshrc 沒有生效?

原因:需要重新載入配置

解決

# 方法 1:重新載入配置
source ~/.zshrc

# 方法 2:重啟終端機

Q4: Bash 和 Zsh 可以共存嗎?

:可以!它們只是不同的程式,可以隨時切換:

# 查看預設 shell
echo $SHELL

# 臨時切換
bash   # 切到 bash
zsh    # 切到 zsh
exit   # 回到原本的 shell

# 永久更改預設
chsh -s /bin/bash  # 改為 bash
chsh -s /bin/zsh   # 改為 zsh

Q5: 如何升級 macOS 的 Bash?

macOS 內建的 Bash 是 3.2(很舊),可以用 Homebrew 升級:

# 安裝最新版 Bash
brew install bash

# 查看版本
/usr/local/bin/bash --version
# 輸出:GNU bash, version 5.x.x

# 加入到可用 shell 清單
sudo vim /etc/shells
# 加入這行:/usr/local/bin/bash

# 設為預設(可選)
chsh -s /usr/local/bin/bash

快速決策指南

我該用 Bash 還是 Zsh?

情境 推薦 原因
macOS 日常使用 Zsh 系統預設,功能強大
寫可移植的腳本 Bash 相容性最好
學習 Shell 腳本 Bash 教學資源最多
追求最佳體驗 Zsh + Oh My Zsh 功能最豐富
伺服器管理 Bash Linux 標準

檢查清單

查看你的 Shell 環境

執行以下命令來了解你的系統:

# 1. 當前使用的 Shell
echo $SHELL

# 2. 當前 Shell 進程
echo $0

# 3. Shell 版本
bash --version
zsh --version

# 4. 系統可用的 Shell
cat /etc/shells

# 5. 配置文件是否存在
ls -la ~ | grep -E '\.(bash|zsh)'

# 6. 是否安裝 Oh My Zsh
ls -la ~/.oh-my-zsh

總結

問題 答案
macOS 預設 Shell? Zsh(2019+ Catalina 之後)
Bash 和 Zsh 最大差異? Zsh 功能更強、自動補全更好,但語法略有不同
需要學兩種嗎? 日常用 Zsh,寫腳本了解 Bash 即可
如何切換? chsh -s /bin/zshchsh -s /bin/bash
配置文件在哪? Zsh: ~/.zshrc、Bash: ~/.bashrc
推薦新手? 使用 Zsh + Oh My Zsh,體驗最好

推薦配置:完整的 .zshrc

# ~/.zshrc 範例配置

# Oh My Zsh 設定
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="robbyrussell"

# 插件
plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
    docker
    kubectl
)

source $ZSH/oh-my-zsh.sh

# 環境變數
export PATH="$HOME/bin:/usr/local/bin:$PATH"
export EDITOR=vim
export LANG=en_US.UTF-8

# 別名
alias ll='ls -lah'
alias gs='git status'
alias ..='cd ..'
alias ...='cd ../..'

# Go 環境(如果有用 Go)
export GOPATH="$HOME/go"
export PATH="$PATH:$GOPATH/bin"

# 自動補全設定
autoload -Uz compinit
compinit

# 歷史紀錄
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history
setopt share_history
setopt hist_ignore_dups

# 拼寫檢查
setopt correct

# 載入共用配置(如果有)
if [ -f ~/.shell_common ]; then
    source ~/.shell_common
fi

快速開始

如果你是 Zsh 使用者(macOS 預設)

# 1. 確認使用 Zsh
echo $SHELL  # 應該顯示 /bin/zsh

# 2. 安裝 Oh My Zsh(可選但推薦)
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 3. 編輯配置
vim ~/.zshrc

# 4. 重新載入
source ~/.zshrc

如果你想用 Bash

# 1. 切換到 Bash
chsh -s /bin/bash

# 2. 重啟終端機

# 3. 編輯配置
vim ~/.bashrc

# 4. 在 ~/.bash_profile 中載入 ~/.bashrc
echo "source ~/.bashrc" >> ~/.bash_profile

建議:如果你是 Mac 使用者,直接用 Zsh + Oh My Zsh 是最佳選擇!


建立日期:2024-10-15 最後更新:2025-11-18

🔗相關文章