Bash tool escapes ! to \! when adjacent to whitespace

Resolved 💬 4 comments Opened Mar 10, 2026 by treerz Closed Mar 31, 2026

Description

The Bash tool inserts a backslash before ! when it is adjacent to whitespace, even inside single quotes and in a non-interactive shell where history expansion is already disabled.

Reproduction

printf '%s' ' != ' | xxd
# Expected: 20 21 3d 20  ( != )
# Actual:   20 5c 21 3d 20  ( \!= )

Pattern

| Input | Output | Correct? |
|-------|--------|----------|
| ' != ' | \!= | BUG |
| '!=' | != | OK |
| '! ' | \! | BUG |
| 'x!' | x! | OK |
| 'x! ' | x\! | BUG |
| 'a!b' | a!b | OK |

! is escaped when adjacent to whitespace, but preserved when between non-whitespace characters.

Impact

Breaks any command-line tool or DSL that uses != with standard spacing, as the tool receives \!= instead.

Environment

  • Claude Code 2.1.72
  • bash 5.2.15(1)-release, macOS Darwin 21.6.0
  • Shell is non-interactive, histexpand is off

Analysis

The escaping happens in the Bash tool layer before the command reaches the shell. It appears to be an overzealous history expansion prevention that doesn't account for single-quoted contexts or non-interactive shells where ! is already literal.

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗