如果你還在用 Bash 處理 JSON、CSV 或者系統日誌,那你可能正在用鎚子做手術刀的工作。傳統的 Unix Shell 設計於 1970 年代,那時資料主要是純文字。但 2026 年的世界,資料是結構化的 —— JSON API、CSV 匯出、資料庫查詢結果,全都是表格和記錄。

Nushell 就是為這個時代而生的 Shell。它不是另一個花俏的主題或提示字元增強器,而是一個範式轉變:把資料當資料處理,而不是文字

什麼是 Nushell?

Nushell(簡稱 nu)是一個現代 Shell,由前 Facebook 工程師 Jonathan Turner 建立。它的核心理念很簡單但具革命性:

  • 結構化輸出:每個命令輸出都是結構化資料(表格、清單、記錄),不是純文字
  • 型別安全:Shell 理解資料型別,字串、整數、日期、檔案大小都有明確型別
  • 跨平台:原生支援 Windows、macOS、Linux,無需 WSL 或 Cygwin
  • Rust 編寫:效能優秀,記憶體安全,單一二進制檔部署

截至 2026 年 3 月,Nushell 已經發布到 0.111.0 版本,GitHub 星標超過 35,000+,成為成長最快的 Shell 專案之一。

為什麼你需要 Nushell?

傳統 Shell 的痛點

在 Bash 中處理結構化資料是什麼樣的?

# 從 JSON 中提取特定欄位?需要 jq
curl -s https://api.example.com/users | jq '.[] | select(.active==true) | .name'

# 統計 CSV 中某欄的平均值?需要 awk
cat data.csv | awk -F',' 'NR>1 {sum+=$3; count++} END {print sum/count}'

# 列出大於 100MB 的檔案並排序?管道地獄
ls -lh | awk '$5 ~ /M/ {print $0}' | sort -k5 -h

每個任務都需要不同的工具,管道裡充滿了 awksedgrepjq 的混合體。錯誤處理?型別轉換?祝你好運。

Nushell 的方式

# 同樣的任務,統一的語法
# JSON API 查詢
http get https://api.example.com/users | where active == true | select name

# CSV 資料分析
open data.csv | math avg --columns price

# 檔案過濾和排序
ls | where size > 100MB | sort-by size

看到區別了嗎?一致的語法,結構化的資料流,無需外部工具

安裝 Nushell

Linux (推薦方式)

# 使用官方安裝腳本
curl --proto '=https' --tlsv1.2 -sSf https://www.nushell.sh/install.sh | sh

# 或使用套件管理器
# Ubuntu/Debian
cargo install nu

# Arch Linux
yay -S nushell

# Fedora
sudo dnf install nushell

macOS

# Homebrew
brew install nushell

# MacPorts
sudo port install nushell

Windows

# Winget (Windows 10/11)
winget install nushell

# Chocolatey
choco install nushell

# Scoop
scoop install nushell

驗證安裝

nu --version
# 輸出:nu 0.111.0

核心概念:從文字到資料

1. 表格是第一公民

在 Nushell 中,ls 不再輸出文字行,而是回傳表格:

ls
# ╭───┬──────────────────┬──────┬────────┬───────────────╮
# │ # │       name       │ type │  size  │   modified    │
# ├───┼──────────────────┼──────┼────────┼───────────────┤
# │ 0 │ docs             │ dir  │  4.0KB │ 10 minutes ago│
# │ 1 │ README.md        │ file │  2.3KB │ 2 hours ago   │
# │ 2 │ package.json     │ file │  1.1KB │ 1 day ago     │
# ╰───┴──────────────────┴──────┴────────┴───────────────╯

這是一個真正的表格物件,你可以:

# 過濾
ls | where type == file

# 選擇欄
ls | select name size

# 排序
ls | sort-by size -r

# 獲取特定行
ls | get 0.name

2. 資料型別系統

Nushell 理解並尊重資料型別:

# 數字運算
echo 10 + 5        # 15 (整數)
echo 3.14 * 2      # 6.28 (浮點數)

# 字串操作
echo "hello" | str length           # 5
echo "Hello World" | str downcase   # hello world

# 日期處理
date now
date now | format date "%Y-%m-%d"
date now - date "2026-01-01"  # 顯示天數差

# 檔案大小(自動轉換)
echo 1GB / 1MB  # 1024

3. 管道傳遞的是值,不是文字

這是最關鍵的差異。在 Bash 中:

# 文字流,每個命令都要解析文字
ls -l | grep ".md" | awk '{print $9}'

在 Nushell 中:

# 結構化資料流,保持型別資訊
ls | where name =~ ".md" | get name

實戰:資料管道範例

範例 1:分析 GitHub 儲存庫資訊

# 獲取儲存庫資料並分析
http get https://api.github.com/repos/nushell/nushell \
  | select stargazers_count forks_count open_issues language \
  | flatten

範例 2:處理 CSV 銷售資料

假設你有一個 sales.csv

date,product,amount,region
2026-01-01,Widget A,1500,North
2026-01-02,Widget B,2300,South
2026-01-03,Widget A,1800,North
# 讀取並分析
open sales.csv \
  | where region == "North" \
  | group-by product \
  | each { |it| 
      {
        product: $it.product.0,
        total: ($it | get amount | math sum),
        count: ($it | length)
      }
    }

範例 3:系統監控儀表板

# 建立系統狀態報告
{
  hostname: (sys host | get name),
  uptime: (sys host | get uptime),
  memory: {
    total: (sys mem | get total),
    free: (sys mem | get free),
    percent_used: ((sys mem | get total) - (sys mem | get free)) / (sys mem | get total) * 100
  },
  disk: (sys disks | select name mount total free | 
         insert percent_used { |it| (($it.total - $it.free) / $it.total * 100) | math round })
}

範例 4:批次重新命名檔案

# 將所有 .jpeg 檔案重新命名為 .jpg
ls *.jpeg \
  | each { |it| 
      mv $it.name ($it.name | str replace ".jpeg" ".jpg")
    }

2026 新特性亮點

Nushell 0.110.0 和 0.111.0 帶來了重大改進:

1. 管道中的 let 命令

現在可以在管道中間定義變數:

ls 
  | let total = (length)
  | let files = (where type == file | length)
  | echo $"Total: ($total), Files: ($files)"

2. 命令群組別名

減少輸入,提高效率:

# 定義別名群組
alias gs = git status
alias ga = git add
alias gc = git commit

# 或使用命令群組
def "git s" [] { git status }
def "git a" [] { git add }

3. 增強的 finally 支援

確保清理程式碼總是執行:

try {
  open data.json | process
} catch { |err|
  echo $"Error: ($err)"
} finally {
  echo "Cleanup complete"
}

4. Polars 外掛程式整合

大資料處理效能提升:

# 使用 Polars 外掛程式處理大型 CSV
polars open large_dataset.csv 
  | polars filter (polars col "amount" > 1000)
  | polars group-by "region"
  | polars agg (polars col "amount" | polars agg sum)

設定和客製化

設定檔位置

# 設定檔
~/.config/nushell/config.nu

# 環境變數
~/.config/nushell/env.nu

# 歷史記錄
~/.local/share/nushell/history.txt

基本設定範例

# config.nu

# 設定編輯器
$env.EDITOR = "nvim"

# 自訂提示字元
def create_prompt [] {
  let path = (pwd | path basename)
  $"[($path)]> "
}

$env.PROMPT_COMMAND = { create_prompt }

# 啟用语法定位
use ~/.config/nushell/plugins/gghid.nu *

有用的別名

# 新增到 config.nu

alias ll = ls -l
alias la = ls -a
alias lg = ls | grep
alias rm = rm -i  # 安全刪除

# Git 快捷方式
alias gs = git status
alias ga = git add
alias gc = git commit -m
alias gp = git push

效能對比

根據 2026 年的基準測試:

任務 Bash + 工具 Nushell
JSON 解析 jq + 管道 原生 (快 3 倍)
CSV 處理 awk/csvkit 原生 (快 2 倍)
檔案過濾 find + grep ls + where (相當)
啟動時間 ~10ms ~50ms
記憶體佔用 ~5MB ~15MB

Nushell 的啟動稍慢,但在複雜資料處理任務中表現更優,因為減少了外部行程呼叫。

學習資源

何時使用 Nushell?

✅ 適合的場景

  • 處理 JSON、CSV、YAML 等結構化資料
  • 建構資料管道和 ETL 流程
  • 跨平台腳本開發
  • API 資料獲取和處理
  • 系統管理和監控任務

⚠️ 需要謹慎的場景

  • 現有 Bash 腳本遷移(需要重寫)
  • 極度效能敏感的迴圈(啟動開銷)
  • 依賴特定 Shell 行為的生產環境
  • 與舊工具鏈深度整合的系統

總結

Nushell 代表了 Shell 設計的未來方向。它不是要完全取代 Bash(系統腳本還是需要 POSIX 相容性),而是為現代開發任務提供了一個更強大的互動式和腳本環境。

關鍵優勢

  1. 🎯 結構化資料優先 — 不再需要 jqawksed 的混合體
  2. 🔒 型別安全 — 減少腳本錯誤和意外行為
  3. 🚀 跨平台一致 — 同一腳本在所有平台執行
  4. 📦 單一二進制檔部署 — 無需依賴,易於散布
  5. 🛠️ 活躍開發 — 2026 年持續更新,社群成長迅速

如果你經常處理 API、資料檔案,或者只是想要一個更現代化的命令列體驗,Nushell 值得投入時間學習。


開始使用:安裝後執行 nu 進入互動模式,或執行 nu script.nu 執行腳本。官方教學是最佳起點。