Skip to content

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:

text
> Run the project's tests

Claude Code will:

  1. Determine the test command from CLAUDE.md (e.g. pytest tests/ -x)
  2. Call the Bash tool to execute
  3. Analyze the output
  4. If there are failing tests, automatically fix and re-run
text
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:

LevelBehaviorExample
Auto-allowRead-only operationsls, cat, grep, find, git status
Requires confirmationWrite operationspip install, npm install, git commit
Default denyHigh-risk operationsrm -rf /, `curl

Configuring a Whitelist ​

Pre-approve common commands in the project .claude/settings.json:

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:

bash
claude --sandboxed

In 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:

text
> Run docker-compose up and check status after 60 seconds

Claude 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 ​

text
> Run the full check流程: lint first, then type check, then run tests. Fix anything that fails.

Git Operations ​

text
> Commit the current changes; the commit message should clearly describe what was changed and why

Environment Setup ​

text
> Check whether the Python version is 3.12; if not, help me install pyenv and switch to it

Chapter 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.json can 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 ​

  1. Have Claude Code run your project's full test suite.
  2. Configure a whitelist for common commands in settings.json.
  3. Have it commit a Git commit for you (observe the confirmation flow).

After completing, proceed to Chapter 6.