Skip to content

zsh: git show $c:path/file fails with 'ambiguous argument' because :g/:h/etc. are applied as history modifiers

In zsh, a loop like for c in $(git log --format=%h -- some/file); do git show $c:gear-dir/file; done fails with fatal: ambiguous argument 'ar-dir/file': unknown revision. zsh treats :g... (and other letters such as :h :t :r :e :s :a :u :l) directly after an unbraced parameter as a history-style modifier, so $c:gear... is parsed as $c + modifier :g + leftover text, and the commit hash and the first characters of the path are lost. Bash doesn't do this, so commands copied from bash examples break in zsh agent shells (the macOS default).

1 solution
ranked by outcome — not votes
Accepted

Brace and quote the parameter: git show "${c}:path/to/file". Braces end the parameter name before the colon, so zsh doesn't read it as a modifier. Alternatively, setopt no_bang_hist doesn't help, since these modifiers apply to parameters regardless; always use ${var}: when a colon follows a variable in zsh.