Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

The shell prints event not found when a command contains !

Your error

zsh: event not found: /bin/bash1 more line it can show

Fix it fast

Most common cause, 1 of 2

zsh expands ! even inside double quotes

Quote the text with single quotes, or put a backslash before the !.

Fix
echo '#!/bin/bash' > deploy.sh
# or, for the rest of this session:
setopt NO_BANG_HIST
# or, permanently:
echo 'setopt NO_BANG_HIST' >> ~/.zshrc
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

zsh: event not found means the interactive shell read ! as a history expansion, even inside double quotes, and found no earlier command to match, as in echo "#!/bin/bash". Put the text in single quotes, escape the ! (zsh only), or run setopt NO_BANG_HIST in zsh or set +H in bash. Applies to zsh 5.9.x and bash 5.3.

  • Applies to zsh 5.9.2, GNU bash 5.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Check which shell you are typing into and whether ! is still special there. In zsh the option is BANG_HIST; in bash it is the H flag inside $-.

Run this first
echo $0
# zsh: prints "on" when ! starts a history expansion
[[ -o banghist ]] && echo on
# bash: prints H when history expansion is on
echo $- | grep -o H

Then find the line you see in this list and open its fix:

  1. 1zsh expands ! even inside double quotesThe message starts with zsh: and the text after the colon is whatever followed the ! in your command
  2. 2bash expands ! in double quotes, and a backslash stays in the outputThe message starts with bash:, or the line runs after you add \! but the file now contains #\!/bin/bash

What does the error look like?

  • zsh: event not found: /bin/bash
  • bash: !/bin/bash": event not found

Cause 1 of 2

zsh expands ! even inside double quotes

You seeThe message starts with zsh: and the text after the colon is whatever followed the ! in your command

Check

In an interactive zsh, event not found appears because ! starts a history expansion anywhere on the line, double quotes included. Only single quotes, $'...' quotes or a backslash stop it. So echo "#!/bin/bash" asks zsh for the last command that began with /bin/bash, finds none, and refuses to run the line.

Check
setopt | grep -i banghist || echo "banghist is on (the default)"

Fix

Quote the text with single quotes, or put a backslash before the !. To stop zsh treating ! specially in every session, add NO_BANG_HIST to ~/.zshrc.

Fix
echo '#!/bin/bash' > deploy.sh
# or, for the rest of this session:
setopt NO_BANG_HIST
# or, permanently:
echo 'setopt NO_BANG_HIST' >> ~/.zshrc

Verify

The command runs and the file starts with the exact line you typed.

Verify
head -1 deploy.sh

Confirmed by zsh.sourceforge.io

Did this fix it?

Cause 2 of 2

bash expands ! in double quotes, and a backslash stays in the output

You seeThe message starts with bash:, or the line runs after you add \! but the file now contains #\!/bin/bash

Check

Interactive bash also runs history expansion on ! inside double quotes. Escaping it there is a trap: the bash manual says the backslash before the ! is not removed, so the shebang is written as #\!/bin/bash and the script no longer runs with the right interpreter. Scripts are not affected, because bash does not expand history in non-interactive shells by default.

Check
grep -n '\\!' deploy.sh

Fix

Use single quotes, which quote ! fully in bash. If you paste many commands that contain !, turn history expansion off for the session with set +H, or add it to ~/.bashrc.

Fix
echo '#!/bin/bash' > deploy.sh
# or turn it off for this shell:
set +H
echo "#!/bin/bash" > deploy.sh

Verify

The first line of the file is #!/bin/bash with no backslash, and the script runs.

Verify
head -1 deploy.sh
bash deploy.sh

Confirmed by man7.org

Did this fix it?

How do I stop it coming back?

  • Write shebang lines and other text that holds ! with an editor or with single quotes, not with echo and double quotes.
  • Wrap passwords, URLs and commit messages that contain ! in single quotes when you type them at a prompt.
  • If your team pastes commands from a runbook into zsh, add setopt NO_BANG_HIST to the shared dotfiles and note it in the runbook.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.