PromptABCD
FeaturesLearnHow it worksUse casesFAQGuideBlogContext Blocks
Sign inGet started free
Sign inSign up
PromptABCD

A calm home for your best AI prompts. Save them once, find them in seconds, reuse them forever.

Product

  • Features
  • Chrome Extension
  • Free Courses
  • How it works
  • Use cases
  • Blog
  • Context Blocks
  • Export Anywhere
  • FAQ

Resources

  • User guide
  • Learn prompting
  • Sign in
  • Get started free

© 2026 PromptABCD. All rights reserved.

Privacy PolicyTerms and Conditions
Home/Blog/CLI AI Agents/Running Shell Commands With a CLI Agent Safely
CLI AI Agents

Running Shell Commands With a CLI Agent Safely

The scary part of cli agent shell commands isn't the obviously destructive command — it's the plausible one with a wrong flag, approved on autopilot. Here's how to allowlist by pattern, deny the catastrophic, and sandbox the rest.

September 11, 2026·9 min read
ShareShare
⚡Featured Prompt— copy and use right now
# The overloaded setup: full shell, no gate
claude --permission-mode bypassPermissions
# or the Aider equivalent of just saying yes to every /run

Here's the counterintuitive part about letting an agent run your terminal: the dangerous command is almost never the one that looks dangerous. Nobody approves

rm -rf /
. The command that actually burns you is the plausible one — a
git push --force
to the wrong branch, a
psql
against prod instead of staging, a migration run twice. When people worry about cli agent shell commands, they picture cartoon destruction. The real risk is quieter: a reasonable-looking command with one wrong flag, approved on autopilot because it looked fine.

Once you understand that, the whole approach to shell access changes. Let's tear down the weak way people set this up and build the version that's actually safe.

Before: The Weak Way to Approve Commands

The default instinct is blanket approval. You get tired of confirming every command, so you flip the switch that says "stop asking."

hljs bash
[object Object],
claude --permission-mode bypassPermissions
,[object Object],

What this does: Runs the agent with permission checks off, so it executes any shell command without a prompt. It feels productive for about ten minutes — right up until the agent runs something you'd never have approved if you'd read it.

The problem isn't that the agent is malicious. It's that an agent optimizing to complete a task will reach for whatever command gets there, and "get there" and "safe" aren't the same goal. Ask it to "clean up the branches" and it may cheerfully delete a branch you hadn't merged. It did exactly what you said. That's the whole problem.

⚠️ Common mistake: Treating bypass mode as a time-saver. It trades a two-second confirmation for an unbounded downside. The correct trade isn't "approve everything" versus "approve nothing" — it's approving categories of command once, so the routine stuff flows and the dangerous stuff still stops.

Why It Fails: Approval Fatigue Is the Attack Surface

Blanket approval fails for a human reason, not a technical one. When every command needs a yes, you stop reading them. By the fiftieth

npm test
confirmation, you're hitting enter without looking — which means the one command that mattered, buried in the stream, gets the same reflexive yes as the forty-nine safe ones.

This is why "just review each command" doesn't scale as a safety strategy. Humans are bad at staying vigilant across dozens of near-identical decisions. Any system for cli agent shell commands that depends on you carefully reading every single one will fail exactly when it matters, because that's when you're deepest in the reflex.

The fix is to move the decision from runtime to setup time. Decide once, when you're thinking clearly, which categories of command are always fine, which always need a look, and which are simply forbidden. Then the agent runs freely inside the safe set and only interrupts you for the genuinely consequential calls.

After: Allowlist by Pattern, Not by Blanket

The strong setup grants specific command patterns instead of all-or-nothing access. Claude Code supports scoped tool rules that match shell commands by shape, so you can say "read-only git is always fine" without opening the door to everything.

hljs bash
claude --allowed-tools ,[object Object], ,[object Object], ,[object Object], ,[object Object], ,[object Object],

What this does: Lets the agent read files, search, run

git diff
with any arguments, check status, and run the test suite — all without prompting — while every command outside that list still stops for approval. The routine flow is frictionless; anything novel gets a human.

The mirror image is the denylist, and it's the more important half. Some commands should never run unattended regardless of context.

hljs bash
[object Object],
{
  ,[object Object],: {
    ,[object Object],: [,[object Object],, ,[object Object],, ,[object Object],]
  }
}

What this does: Refuses force-pushes, recursive deletes, and anything mentioning

prod
, even if a broader allow rule would otherwise permit it. Deny rules take precedence, so this is your hard floor — the commands that stay off the table no matter how the task is phrased.

⚡ Pro tip: Write your deny list from real incidents, not imagination. The first time an agent does something you had to undo, add that command shape to the deny rules. Within a few weeks your denylist encodes your team's actual scar tissue, which is far more accurate than a generic list you copied from a blog.

Breaking Down Each Element of Safe CLI Agent Shell Commands

Four pieces make shell access safe, and they compose.

The allowlist handles routine throughput. Read-only inspection, tests, formatters,

git diff
— the commands you'd approve a thousand times in a row get approved once as a pattern.

The denylist handles the catastrophic set. Force pushes, destructive deletes, anything touching production. These never run without you, full stop.

The sandbox handles the unknown middle. Cursor's CLI ships a network-denied-by-default sandbox configured through

sandbox.json
, and Codex CLI runs commands inside OS-level sandboxing. Both mean a command you didn't anticipate still can't reach the network or escape its box.

hljs bash
[object Object],
cursor-agent -p ,[object Object], --mode plan

What this does: Runs the agent in read-only plan mode first, so it tells you which commands it intends to run before any of them execute. Combined with the sandbox, an unexpected command is contained even if you approve it by reflex.

The turn limit handles runaway loops. A

--max-turns
cap stops an agent that's stuck retrying a failing command from burning your whole quota — or your whole afternoon — in one unattended run.

hljs bash
claude -p ,[object Object], --permission-mode dontAsk --max-turns 5

What this does: Runs a bounded, non-interactive session that pre-approves the allowlisted tools but stops after five turns. For unattended automation,

dontAsk
respects your allow and deny rules without prompting, and the turn cap guarantees the run can't spiral.

The Command Category Everyone Forgets: Network Calls

Ask people to name dangerous commands and they list file deletions and force pushes. Almost nobody names

curl
,
wget
,
pip install
, or
npm install
— and those are the ones that quietly turn a coding agent into an exfiltration or supply-chain risk. A command that reaches the network can send your code somewhere, or pull an unpinned package that runs arbitrary install scripts. Neither looks scary in a diff.

This is exactly why the network-denied-by-default sandboxes matter more than they first appear. When an agent can't reach the network unless you explicitly allow it, a task that only needed to edit files and run tests simply can't phone home, no matter what command it constructs.

hljs bash
[object Object],
{
  ,[object Object],: {
    ,[object Object],: [,[object Object],, ,[object Object],, ,[object Object],, ,[object Object],]
  }
}

What this does: Blocks the agent from making network calls or installing packages without an explicit exception. Most coding tasks never need these, so denying them by default costs you nothing and closes the quietest hole in your setup.

The nuance is that some tasks legitimately need a package installed. The right move isn't to permanently allow installs — it's to grant the exception for that one session, watch what it installs, and revoke it after. Standing network access for a coding agent is almost never justified.

⚡ Pro tip: Treat any agent request to install a package as a review checkpoint, not a rubber stamp. Ask which package and why, and pin the version. Agents will confidently reach for a package to solve a problem your existing dependencies already handle, adding supply-chain surface you didn't need.

⚡ Pro tip: Run agents that don't need the internet with the network off entirely. A refactor, a bug fix, a test-writing task — none of these need to reach out. Turning the network off for the whole session is the simplest, strongest version of the denylist, and it makes exfiltration impossible rather than merely disallowed.

Variations for Different Contexts

A DevOps engineer wiring an agent into CI: allowlist the read-only and test commands, deny everything that mutates infrastructure, and run headless with a strict turn cap. The pipeline agent can diagnose and propose, but a human merges the PR.

A data scientist running an agent against notebooks and datasets: deny any command matching the production database connection string, allowlist the local analysis tools, and keep network access off unless a specific task needs it. The agent can slice data all day and never phone home.

A backend developer doing local feature work: run interactively with a generous allowlist for the dev loop (

git diff
, tests, the local server) and let anything unusual prompt. You get flow on the routine stuff and a real checkpoint on the rest.

A platform team supporting fifty engineers: put the allow and deny rules in a committed settings file and an enterprise-managed policy layer so the denylist can't be relaxed by an individual. The categories that must never run are enforced org-wide, not left to each person's discipline.

⚡ Pro tip: Scope your allowlist to the narrowest pattern that still flows.

Bash(git diff *)
is safe;
Bash(git *)
quietly includes
git push
and
git reset --hard
. The tighter the pattern, the smaller the surface area you're trusting, and the more often a genuinely novel command stops for the look it deserves.

Save and Reuse This

Safe cli agent shell commands come down to one shift: stop deciding at runtime and start deciding at setup. Allowlist the routine command patterns so throughput stays high, denylist the catastrophic ones so they never run unattended, sandbox the unknown middle so surprises stay contained, and cap turns so nothing spirals. Blanket approval isn't the fast path — it's the slow path with a delayed bill.

The allow rules, deny rules, and sandbox configs that work for your stack are the same across every repo you touch, so write them once and reuse them. Keeping those permission templates in PromptABCD means a new project inherits your hard-won guardrails on day one instead of relearning them the expensive way.

cli agent shell commandscli ai agentsagent permissionsclaude codecursor cliterminal automation

Continue Reading

Using a CLI Agent for Large Refactors
CLI AI Agents

Using a CLI Agent for Large Refactors

One prompt to refactor the whole codebase is how agents bury silent bugs. Safe cli agent refactoring writes characterization tests first, then migrates one small reviewable commit at a time.

September 11, 2026·9 min read
CLI Agent Workflows for Bug Fixing
CLI AI Agents

CLI Agent Workflows for Bug Fixing

Most bug-fixing advice is wrong about step one. Effective cli agent bug fixing reproduces the bug with a failing test first, then fixes against it — replacing the agent's guessing with checking.

September 11, 2026·9 min read
How to Give a CLI Agent Access to Your Repo
CLI AI Agents

How to Give a CLI Agent Access to Your Repo

How much of your repo should an AI agent see? Getting cli agent repo access right means excluding secrets, starting read-only, and scoping writes to an isolated worktree. A fintech case study shows the safe sequence.

September 11, 2026·9 min read

Save the prompts from this post

PromptABCD is a free prompt manager. Paste, organize, and reuse your best AI prompts — no more hunting through chat history.

Start free →
← PreviousHow CLI Agents Edit Files Safely (Without Deleting Your Code)Next →Best CLI AI Agents for Developers in 2026
Share this post:
ShareShare