跳轉到主要內容
這個單元解決什麼問題slash command 把一段常用 prompt 或流程綁成 /name 一鍵叫用。它和 Skill 的關鍵差別是:command 由你主動觸發,Skill 由模型自動判斷是否叫用。本單元講 command 的格式、製作方式、$ARGUMENTS 參數注入,以及和 Skill 的分工判準。

學習目標

  • 說出 command(主動觸發)與 Skill(模型自動叫用)的差別與分工。
  • .claude/commands/~/.claude/commands/ 下建立自訂 command 檔。
  • $ARGUMENTS$0$1 等語法接收參數,讓 command 具備動態能力。
  • disable-model-invocation: true 限制 Claude 自動叫用特定 command。
  • 判斷一個需求該做成 command 還是 Skill,並說出判準。

1. command 是什麼

slash command 是你在 Claude Code session 開頭打 /name 觸發的封裝 prompt。它不依賴模型主動識別意圖,你打什麼就跑什麼(截至 2026-06,依官方 commands 章節 [1])。 三個關鍵特性:
  1. 主動觸發:你不打,Claude 也不會建議打。決定權完全在你。
  2. 可重用:寫一次,之後每次都一樣的叫用方式。
  3. 可版控:放在 .claude/commands/ 內可進 Git,團隊共享。
command 與一般 prompt 的差異一段 prompt 你可以重複貼,但每次要自己手剪參數、調整措詞。command 把這件事打包:你只需要打 /<name> <args>,後面邏輯一致。這個「一致性」在固定輸出格式(commit message、PR 摘要、changelog)或反覆跑的檢查流程上特別值。

2. 與 Skill 的差別:主動 vs 自動

.claude/commands/<name>.md 與 Skill(SKILL.md)在 Claude Code 裡現在已合併為同一套底層機制 [2]。但兩者觸發時機不同:
維度CommandSkill
觸發方式使用者主動打 /name模型依 description 語意判斷叫用
預設可見性/ 選單顯示,使用者輸入模型可見,使用者也可打 /name
寫在哪裡.claude/commands/<name>.md~/.claude/commands/<name>.md.claude/skills/<name>/SKILL.md~/.claude/skills/<name>/SKILL.md
同名衝突若 skill 與 command 同名,skill 優先 [2]同左
適合場合「我知道現在要做 X」「讓模型判斷什麼時候做 X」
簡化版決策:
  • 確定性高、流程固定 → command。
  • 有副作用、你不希望模型自己決定何時跑(deploy、發送訊息、commit)→ command + disable-model-invocation: true
  • 依使用者意圖判斷才合適(code review、文件搜尋、refactor 建議)→ Skill。
帶副作用的工作流預設用 command部署、推送、發送 Slack、寫入外部系統;任何「做了就難以撤回」的事,預設用 command + disable-model-invocation: true。你不需要 Claude 因為「看起來該發」就自己發。

3. 製作:檔案位置與基本結構

放路徑(截至 2026-06):
位置適用範圍
~/.claude/commands/<name>.md所有專案通用
./.claude/commands/<name>.md當前專案,透過版控共享給團隊
<plugin>/commands/<name>.md在已啟用 plugin 內可用
command 檔的命名就是 / 後面要打的字:.claude/commands/deploy.md 對應 /deploy.claude/commands/fix-issue.md 對應 /fix-issue

3.1 最簡形式

You are helping me write a commit message for the current changes. Use the project's existing commit style (Conventional Commits). Output only the commit message, no preamble.
/commit 觸發後,這段就會被注入 Claude 當下對話,Claude 用它產生 commit message。

3.2 用 frontmatter 設定行為

---
description: Generate a commit message for staged changes
argument-hint: [optional context]
allowed-tools: Bash(git status *) Bash(git diff *) Bash(git log *)
model: sonnet
---

## Staged changes
!`git diff --cached`

## Recent commit style
!`git log --oneline -10`

## Task
Write a Conventional Commits message for the staged changes above.
$ARGUMENTS
幾個常用 frontmatter 欄位(截至 2026-06,依官方 Skills 與 commands 章節 [1, 2]):
欄位用途
description/ 選單顯示、Claude 判斷語意時的提示
argument-hint自動完成時的提示文字,例如 [issue-number]
allowed-tools列出 command 啟用時不需要額外確認的工具
model指定使用模型,預設繼承當前 session
disable-model-invocation: true禁止 Claude 自動叫用此 command
user-invocable: false/ 選單隱藏,只讓模型叫用
為什麼要 disable-model-invocation: true假設你寫了 /deploy command。如果不限制,模型可能在對話中看到「這段程式碼看起來該部署了」就自己叫用 /deploy,然後跑了部署;你可能還在 review。設了 disable-model-invocation: true 之後,這個 command 只在你明確打 /deploy 時才會跑,模型完全看不到它。對 deploy、send、push 這類有副作用的工作流,預設都該加。

4. 參數注入:$ARGUMENTS 與索引語法

command 觸發時,你打 /name 後面這些字 會被注入到 command 內容中。
1

最簡單的接收方式:$ARGUMENTS

$ARGUMENTS 接收你在 /name 後面打的所有文字:
---
description: Fix a GitHub issue by number
---

Fix GitHub issue $ARGUMENTS following our coding standards.
1. Read the issue description
2. Understand the requirements
3. Implement the fix
4. Write tests
5. Create a commit
/fix-issue 123 時,$ARGUMENTS 被替換為 123,Claude 收到的是「Fix GitHub issue 123 following our coding standards. …」[2]。如果 command 內容裡沒有 $ARGUMENTS,Claude Code 會把參數以 ARGUMENTS: <你的輸入> 形式附加在 command 內容尾端,Claude 還是看得到 [2]。
2

位置索引:$0、$1、$2

需要把多個參數拆開處理時,用 $0$1$2$ARGUMENTS[0] 等:
---
description: Migrate a component from one framework to another
---

Migrate the $0 component from $1 to $2.
Preserve all existing behavior and tests.
/migrate-component SearchBar React Vue 時,$0SearchBar$1React$2Vue多字參數要用引號包起來:/migrate-component "Search Bar" React "Vue 3"
3

具名參數

frontmatter 設 arguments 後,內容裡可以用名字取:
---
description: Create a commit with branch and issue context
arguments:
  - branch
  - issue
---
Create a commit on branch $branch referencing issue $issue.
/commit feature-x 456 時,$branchfeature-x$issue456 [2]。

4.1 其他可用變數

變數用途
${CLAUDE_SESSION_ID}當前 session ID,用於日誌或 session 特定檔名
${CLAUDE_SKILL_DIR}command/skill 所在目錄,用於引用同目錄腳本
${CLAUDE_EFFORT}當前 effort 等級(low / medium / high / xhigh / max

5. 動態 context 注入:!`command`

command 內容裡可以用 !`shell command`送進 Claude 之前先執行 shell 指令,把輸出嵌入內容 [2]:
## Current changes
!`git diff HEAD`

## Status
!`git status --short`
當 command 被叫用時,Claude Code 依序執行每個 !`cmd`,把輸出取代進去,然後才把整段內容當作 prompt 交給 Claude。對 Claude 來說,它看到的是已經包含 git diff 結果的純文字。 多行指令用 fenced code block 形式:
## Environment
```!
node --version
npm --version
git status --short
```
動態注入的兩個陷阱
  1. 執行環境就是你的 shell。你打 /deploy 時,command 內的 !`cmd` 是用你的本機 shell 執行的(macOS/Linux 預設 bash,Windows 預設 PowerShell 可切換 [2])。敏感操作別寫在 command 裡。
  2. 禁用設定:組織可在 settings.json"disableSkillShellExecution": true,把所有動態注入替換為 [shell command execution disabled by policy]。寫公開 repo 的 command 時不要依賴這個機制跑關鍵步驟。

6. 工具對照

概念Anthropic Claude(主範本)OpenAI CodexGitHub CopilotCursor
自訂 command 位置.claude/commands/<name>.md~/.claude/commands/<name>.mdconfig.toml 設定 [需確認原始出處:Codex custom slash command 截至 2026-06 的官方路徑].github/prompts/<name>.prompt.md(prompt files 機制).cursor/commands/<name>.md
參數注入語法$ARGUMENTS$0$1、具名變數不適用一般 ARGUMENTS 語法frontmatter 結構化欄位$ARGUMENTS 風格變數
全域 vs 專案作用域~/.claude/commands/ vs .claude/commands/~/.codex/prompts/ vs .codex/prompts/全域與專案分檔全域 User Rules + 專案 .cursor/rules/
觸發方式使用者主動輸入 /name使用者主動輸入使用者主動輸入使用者主動輸入
防止模型自動叫用disable-model-invocation: true(機制差異大)(機制差異大)(機制差異大)
命名澄清Claude Code 的 command 與 skill 為同一底層機制:截至 2026-06 兩者已合併,.claude/commands/<name>.md.claude/skills/<name>/SKILL.md 都能產生 /name [2]。差別在前者放單一檔、後者放目錄(含 references/ 與 scripts/,見 04-4 Skill)。本單元講的是兩者共通的「使用者主動觸發」這一面。OpenAI Codex、GitHub Copilot、Cursor 各有「prompt file」或「prompt template」概念,但路徑與機制差異大;通用寫法是「prompt file 支援 $ARGUMENTS 風格變數」,細節以各家當前文件為準。

7. 動手做

30 分鐘練習
  1. 寫一個 /commit command:用 frontmatter 帶 disable-model-invocation: trueallowed-tools: Bash(git *);內容用 !`git diff --cached`!`git log --oneline -10` 取得 staged diff 與最近風格,請 Claude 依 Conventional Commits 格式輸出 commit message。
  2. 建一個全域 command~/.claude/commands/weekly-review.md,用 $ARGUMENTS 接收你想 focus 的主題;啟動新專案 session 測試 /weekly-review 可跨專案叫用。
  3. 測試防誤觸:寫一個 /deploy-prod command,故意disable-model-invocation;在對話中提到「看起來該 deploy 了」觀察 Claude 是否會自己叫用。然後加上 disable-model-invocation: true,重複同一句話,確認這次 Claude 不會自己跑。

8. 常見誤區

反模式清單
  • 把所有流程都做成 command:使用者主動觸發過多,會讓工作流摩擦增加。該讓模型自己判斷的場合(code review、文件搜尋)改用 Skill。
  • description 寫太模糊:自己過三天就忘了這個 command 是做什麼用的。具體描述觸發時機與產出格式,別只寫「幫我做 X」。
  • 沒設 disable-model-invocation 就寫 deploy/push/send:模型可能在你還沒確認時自己跑,產生不可逆副作用。
  • 把含敏感資訊的 command 檔 commit 進公開 repo:command 內容進版控,API key、絕對路徑、內部 URL 都會外洩。
  • 動態注入依賴未驗證狀態:command 裡寫 !`./scripts/deploy.sh` 而該腳本權限或內容不固定,重現性就沒了。需要確定性的部署流程應改用 Hook(見 04-6 Hooks)或獨立 CI 步驟。

自我檢核

通過本單元的標準
  1. 你能說出 command 與 Skill 的根本差別嗎?(觸發主體不同)
  2. 你能在五分鐘內寫出一個能用 $ARGUMENTS 接收參數的 command 嗎?
  3. 你能判斷哪些 command 該加 disable-model-invocation: true 嗎?
  4. 你手邊有沒有一個每週重複打三次以上的 prompt?它該做成 command 還是 Skill?說出你的判準。

來源與延伸閱讀

事實主張依官方文件,快變動項標註截至 2026-05。
  • [1] Anthropic, “Commands,” code.claude.com, 2026. [Online]. Available: https://code.claude.com/docs/en/commands (截至 2026-06;含內建與自訂 command)
  • [2] Anthropic, “Extend Claude with skills,” code.claude.com, 2026. [Online]. Available: https://code.claude.com/docs/en/skills (截至 2026-06;含 command 與 skill 合併機制、$ARGUMENTS 與 frontmatter 完整參考)
  • [3] Anthropic, “How Claude remembers your project,” code.claude.com, 2026. [Online]. Available: https://code.claude.com/docs/en/memory (截至 2026-06;CLAUDE.md 與 rules 模組化)