claude-code - 💡(How to fix) Fix `!` injection: `&&` chain drops commands after sudo (works fine in fresh shell)

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Workaround

Wrap everything inside one sudo invocation so the chain doesn't cross the sudo boundary:

! sudo bash -c 'cmd1 && cmd2 && cmd3'

Or split into separate ! calls (each independent sudo invocation).

Code Example

! sudo touch /tmp/repro_a && echo "step2 ran" && touch /tmp/repro_b && echo "step3 ran"

---

! sudo touch /tmp/repro_a && echo "step2 ran" && touch /tmp/repro_b && echo "step3 ran"
! ls /tmp/repro_* 2>&1; echo "---exit: $?---"
/tmp/repro_a
---exit: 1---

---

❯ sudo touch /tmp/chain_a && echo "step2 ran" && touch /tmp/chain_b && echo "step3 ran"; echo "exit: $?"
[sudo] password for elienop:
step2 ran
step3 ran
exit: 0

---

! sudo bash -c 'cmd1 && cmd2 && cmd3'
RAW_BUFFERClick to expand / collapse

Repro

In a Claude Code session, with any sudo cache state (cached or fresh), paste:

! sudo touch /tmp/repro_a && echo "step2 ran" && touch /tmp/repro_b && echo "step3 ran"

Actual

No output. Only /tmp/repro_a is created. step2 ran / step3 ran echoes never appear; /tmp/repro_b is never created. Chain exits 1.

❯ ! sudo touch /tmp/repro_a && echo "step2 ran" && touch /tmp/repro_b && echo "step3 ran"
❯ ! ls /tmp/repro_* 2>&1; echo "---exit: $?---"
/tmp/repro_a
---exit: 1---

Expected

Both files exist, both echoes printed.

Same chain in a fresh shell works correctly

Exact same command in a fresh konsole window (no Claude Code involvement, same user/zsh/sudo config):

❯ sudo touch /tmp/chain_a && echo "step2 ran" && touch /tmp/chain_b && echo "step3 ran"; echo "exit: $?"
[sudo] password for elienop:
step2 ran
step3 ran
exit: 0

So the bug is specifically in how ! shell injection handles && chains that contain a sudo command — not the shell or sudo config.

Sudo cache state doesn't matter

Bug fires whether sudo prompts for a password (cache cleared with sudo -k) or runs silently from a warm cache. So it's not about password input being intercepted.

Environment

  • Claude Code: 2.1.143
  • OS: CachyOS Linux (Arch derivative), kernel 7.0.8-1-cachyos
  • Shell: zsh 5.9 with powerlevel10k
  • sudo: 1.9.17p2

Impact

Multi-step workflows involving sudo silently fail. Hit it ~4 times today during routine operations (file copy + service restart, system upgrades, cleanup chains). The silence is the worst part — easy to think the command succeeded.

Workaround

Wrap everything inside one sudo invocation so the chain doesn't cross the sudo boundary:

! sudo bash -c 'cmd1 && cmd2 && cmd3'

Or split into separate ! calls (each independent sudo invocation).

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix `!` injection: `&&` chain drops commands after sudo (works fine in fresh shell)