Chapter 5 · Bash Execution and Automation
Chapter goals: Master the use of the Bash tool and its permission model, understand the security sandbox mechanism, and learn to automate build/test/deploy flows with Claude Code.
5.1 Bash Tool Basics
Claude Code can execute shell commands directly in the terminal:
> Run the project's testsClaude Code will:
- Determine the test command from CLAUDE.md (e.g.
pytest tests/ -x) - Call the Bash tool to execute
- Analyze the output
- If there are failing tests, automatically fix and re-run
Running pytest tests/ -x --tb=short ...
======================== 14 passed in 2.35s ========================
All tests passed ✅5.2 Permission Model
Bash execution follows a tiered permission strategy:
| Level | Behavior | Example |
|---|---|---|
| Auto-allow | Read-only operations | ls, cat, grep, find, git status |
| Requires confirmation | Write operations | pip install, npm install, git commit |
| Default deny | High-risk operations | rm -rf /, `curl |
Configuring a Whitelist
Pre-approve common commands in the project .claude/settings.json:
{
"permissions": {
"allow": [
"Bash(npm run build)",
"Bash(npm test)",
"Bash(pytest *)",
"Bash(git add *)",
"Bash(ruff check *)"
],
"deny": [
"Bash(rm -rf /*)"
]
}
}5.3 Sandbox Mode
Use the --sandboxed flag to restrict filesystem and network access:
claude --sandboxedIn sandbox mode:
- File writes are limited to the working directory
- Network requests require additional confirmation
- System directories are read-only
Suitable for handling untrusted code or running unpredictable operations.
5.4 Timeout Settings
Long-running commands can have timeouts set:
> Run docker-compose up and check status after 60 secondsClaude Code has a default timeout protection to prevent infinite blocking. You can also specify explicitly in your prompt.
5.5 Common Automation Scenarios
Build + Test + Lint End-to-End
> Run the full check流程: lint first, then type check, then run tests. Fix anything that fails.Git Operations
> Commit the current changes; the commit message should clearly describe what was changed and whyEnvironment Setup
> Check whether the Python version is 3.12; if not, help me install pyenv and switch to itChapter Summary
- The Bash tool lets Claude Code execute shell commands directly and analyze results;
- Three-tier permissions: read-only auto-allowed, write operations require confirmation, high-risk denied;
.claude/settings.jsoncan configure whitelists/blacklists;- Sandbox mode provides an additional security layer.
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. What is Claude Code's default behavior for read-only Bash commands like ls or cat?
2. What does configuring "Bash(pytest *)" in .claude/settings.json mean?
3. What does the --sandboxed flag do?
4. When Claude Code runs tests and finds failures, what is the most typical agentic behavior?
🛠️ Hands-on Practice
- Have Claude Code run your project's full test suite.
- Configure a whitelist for common commands in settings.json.
- Have it commit a Git commit for you (observe the confirmation flow).
After completing, proceed to Chapter 6.