{"slug": "fixing-the-broken-shell-execution-in-claude-code", "title": "Fixing the broken shell execution in Claude Code", "summary": "A developer spent four hours fixing a permissions loop in Claude Code, Anthropic's CLI coding assistant, that blocked an Express.js-to-TypeScript migration with an EACCES error on '/Users/dev/project/node_modules/.bin/tsc'. The fix required killing the process, running 'sudo chown -R $USER .', and restarting the CLI, and the developer found that batching migrations to five files at a time improved success rates from 40% to 90% while reducing token costs.", "body_md": "# Fixing the broken shell execution in Claude Code\n\nI spent four hours last Wednesday fighting a permissions loop that almost made me uninstall [Claude Code](/en/tags/claude%20code/) and go back to a standard IDE plugin. I was trying to automate a migration of a legacy Express.js project to TypeScript, and the CLI kept hitting a wall when trying to run `npm install` after updating the `package.json`.\n\nThe fix wasn't in the prompt, but in how I handled the tool-use permissions. If you're using [Claude](/en/tags/claude/) Code and it keeps asking for permission to run the same command three times, or fails with a cryptic shell error, you're likely hitting the same wall I did.\n\n## Why did the shell keep failing during the migration?\n\nI was running `claude` in a directory with a mix of root-owned files (remnants of a Docker volume mount). When Claude tried to execute a shell command to verify a file change, it hit this:\n\n`Error: EACCES: permission denied, open '/Users/dev/project/node_modules/.bin/tsc'`\n\nThe annoying part? Claude didn't just tell me it failed. It tried to \"fix\" it by suggesting `chmod` commands, which then failed because the CLI session didn't have sudo privileges. I was stuck in a loop where the AI was trying to be helpful, but the environment was locked.\n\nI realized the bottleneck wasn't the AI's logic—it was the session's execution context. I had to kill the process, fix my local folder permissions via `sudo chown -R $USER .`, and restart the CLI.\n\nThe lesson here is that Claude Code is an agent with a shell; it's not just a chat box. If your environment is messy, the agent will spend more tokens trying to fix your OS than fixing your code.\n\n## How to actually get a migration started without the loop\n\nIf you're looking for a Claude Code tutorial that isn't just \"type these commands,\" start with a targeted scope. Don't just say \"convert this to TypeScript.\" That's how you end up with a broken `tsconfig.json` and a thousand linting errors.\n\nHere is the exact sequence I used to successfully migrate my controllers after fixing the permission bug:\n\n1. **Index the project first.** Let it read the files.\n\n2. **Create a specific migration plan.** Ask it to list every file that needs a `.ts` extension.\n\n3. **Execute in batches.**\n\nI found that asking it to do 5 files at a time kept the context window clean. When I tried to do 20 files in one go, it started hallucinating imports from files it hadn't actually converted yet.\n\n| Approach | Result | Token Cost (Est.) | Success Rate |\n\n| :--- | :--- | :--- | :--- |\n\n| \"Convert everything\" | Massive merge conflicts | High | 40% |\n\n| Batch of 5 files | Clean compiles | Medium | 90% |\n\n| File-by-file | Tedious but perfect | Low | 100% |\n\n## Dealing with the \"Too Many Tool Calls\" bottleneck\n\nAnother wall I hit was the tool-call limit. While working on a complex refactor, Claude started looping—reading a file, searching for a string, reading the file again, and then searching for the same string.\n\nIt looked like this in the terminal:`Reading file...`` Searching for 'UserSchema'...``Reading file...`` Searching for 'UserSchema'...`\n\nThis is a waste of money and time. I stopped the execution and shifted my approach toward [AI Coding](/en/category/aicoding/) patterns that emphasize \"Plan first, execute second.\" I told it: \"Stop searching. Read the entire `models/` directory into your context once, then tell me where the schema is.\"\n\nSuddenly, the loop stopped. The trick is to realize that the agent sometimes forgets it already read a file if the conversation gets too long. Forcing a \"consolidated read\" saves you from the repetitive tool-call cycle.\n\n## Where to find better patterns than the default docs\n\nThe official docs are fine for installation, but they don't tell you how to handle the \"AI drift\" that happens during a three-hour coding session. This is where I actually found value in the PromptCube community.\n\nInstead of guessing how to structure a request for a complex refactor, I started looking at [Prompt Sharing](/en/category/prompts/) threads. I found a specific pattern for \"Architectural Guardrails\" that I now paste into my initial session: *\"Before modifying any file, describe the change in one sentence. If the change affects more than three files, stop and ask for confirmation.\"*\n\nThis one line prevents Claude Code from rewriting half your project because it decided to rename a variable globally.\n\n## Recommended setup for a smoother experience\n\nIf you're just starting, don't just run it in a random folder. Set up a dedicated environment.\n\n- **Version Control:** Never run Claude Code on a clean branch. Always have a`git checkout -b ai-experiment` branch. It will delete things. It's a fact.\n- **[MCP](/en/tags/mcp/) Servers:** If you need it to interact with your database or Jira, set up the Model Context Protocol (MCP) servers first. Trying to \"teach\" the AI how to use a tool via chat is inefficient; giving it a formal MCP connection is a game-changer.\n- **Resource Management:** Check out the[Resources](/en/category/resources/) section of the community to see which MCP servers are actually stable. Some of the community-made ones crash the CLI every ten minutes.\n\n`EACCES` or `Permission Denied`, stop the AI, fix the OS, and then resume. Don't let the AI try to fix your Linux permissions unless you've given it a very specific, narrow scope to do so.\n[Next AlphaGenome Atlas is mapping 3 billion base pairs to predict DNA →](/en/threads/9040/)\n\n[these AI tool field notes](https://tanyan888.com/), with plenty of directly applicable cases.", "url": "https://wpnews.pro/news/fixing-the-broken-shell-execution-in-claude-code", "canonical_source": "https://promptcube3.com/en/posts/9100/", "published_at": "2026-09-09 15:58:43+00:00", "updated_at": "2026-09-09 16:23:27.570627+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "ai-agents"], "entities": ["Claude Code", "Anthropic", "Express.js", "TypeScript", "PromptCube"], "alternates": {"html": "https://wpnews.pro/news/fixing-the-broken-shell-execution-in-claude-code", "markdown": "https://wpnews.pro/news/fixing-the-broken-shell-execution-in-claude-code.md", "text": "https://wpnews.pro/news/fixing-the-broken-shell-execution-in-claude-code.txt", "jsonld": "https://wpnews.pro/news/fixing-the-broken-shell-execution-in-claude-code.jsonld"}}