The shell prints event not found when a command contains !
Fix it fast
Most common cause, 1 of 2zsh expands ! even inside double quotes
Quote the text with single quotes, or put a backslash before the !.
echo '#!/bin/bash' > deploy.sh
# or, for the rest of this session:
setopt NO_BANG_HIST
# or, permanently:
echo 'setopt NO_BANG_HIST' >> ~/.zshrcShort 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.
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 $-.
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 HThen find the line you see in this list and open its fix:
- 1zsh expands ! even inside double quotes
The message starts with zsh: and the text after the colon is whatever followed the ! in your command - 2bash expands ! in double quotes, and a backslash stays in the output
The 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/bashbash: !/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.
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.
echo '#!/bin/bash' > deploy.sh
# or, for the rest of this session:
setopt NO_BANG_HIST
# or, permanently:
echo 'setopt NO_BANG_HIST' >> ~/.zshrcVerify
The command runs and the file starts with the exact line you typed.
head -1 deploy.shConfirmed by zsh.sourceforge.io
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.
grep -n '\\!' deploy.shFix
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.
echo '#!/bin/bash' > deploy.sh
# or turn it off for this shell:
set +H
echo "#!/bin/bash" > deploy.shVerify
The first line of the file is #!/bin/bash with no backslash, and the script runs.
head -1 deploy.sh
bash deploy.shConfirmed by man7.org
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.