Intermediate7 min read

Running Bash Commands

Use Claude Code's Bash tool to run tests, install packages, query databases, and automate shell tasks.

The Bash Tool

Claude Code can run shell commands on your machine via the Bash tool. This enables it to run tests, install packages, query databases, check logs, and interact with external services — all within a session.

What Claude Can Run

Anything you could run in a terminal. Common patterns:

Run the test suite and show me which tests are failing.
Install the zod package and update package.json.
Check the git log for the last 10 commits and summarize what changed.

Permissions Model

Before running any shell command, Claude shows you the exact command it plans to execute. You approve or reject it. This prevents surprise side effects.

For frequently-approved commands, you can add them to your allowed list in .claude/settings.json:

{
  "allowedTools": ["Bash(npm test)", "Bash(npm run lint)"]
}

Using Bash for Verification

The most powerful use of the Bash tool is verification loops:

Make the change, then run the tests to verify nothing broke.

Claude will edit the code, run the test suite, read the output, and either report success or iterate on the fix. This tight loop is where Claude Code shines over static code generation tools.

Database Queries

Claude can query your local database to verify state:

Run a query to check how many users were created in the last 24 hours.

It will use whatever CLI tool is available (psql, sqlite3, mysql) and interpret the results.

Safe Bash Patterns

  • Always review commands before approving, especially rm, drop, or DELETE
  • For destructive operations, ask Claude to add a dry-run flag first
  • Use --dangerously-skip-permissions only in sandboxed CI environments, never locally

Environment Variables

Claude inherits your shell's environment variables. If your project needs .env loaded, start the session with source .env && claude or use direnv.

Loading…