The command printf "#!/bin/bash\n" > t generates an error(bash: !/bin/bash\n": event not found), but hitting the up key does not recall the command so that I can modify it. Why is that?

(What I was trying for here can be accomplished with printf "%s\n" '#!/bin/bash' >t I am trying to learn Bash and really want to understand why bash history doesn't capture the previous printf command.)

  • 2
    you need to set +H to make it work Commented Oct 29, 2013 at 14:08
  • The solution I have works. I really just wanted to know why the first thing I tried didn't end up in the bash history. Commented Oct 29, 2013 at 14:36
  • 2
    Reason: see @wingedsubmariner answer. Now as a solution: change it to printf '#!/bin/bash\n' > t . That time as it's within Single Quotes, the "!" won't be interpreted as the special "!" history character and the command will work. Commented Oct 29, 2013 at 18:27
  • Also keep in mind that if HISTIGNORE is set, matching commands (typically starting with a space) will be ignored. This is handy to set and use when you need to use command-line passwords.
    – kmarsh
    Commented Oct 29, 2013 at 19:24
  • 1
    In my shell printf '\n' does indeed print a newline, so @OlivierDulac's suggestion should work. The interpretation of \n as a newline is being done by printf, not when reading the argument, so the kind of quotes doesn't matter. Commented Oct 29, 2013 at 22:07

2 Answers 2


The ! character triggers history expansion, as you discovered. This step occurs before a command is saved to the history, so that it can saved into the history with the expansion already done. When an error occurs in history expansion, bash stops processing the command, and so it never gets saved into the history.

History expansion allows previous commands or parts of previous commands to be substituted into the current command. If this was done before saving the command to the history, it could mean something different each time it is executed, because the previous commands are different in each instance. See http://www.gnu.org/software/bash/manual/bashref.html#History-Interaction

For reference, zsh's behavior is different. It will save the command but with the history expansion missing.

  • Not sure why you got downvoted, but this answered my question very nicely. Thank you. Commented Oct 29, 2013 at 14:20
 bash: !": event not found

This is because, in the default settings for an interactive shell, Bash performs csh-style history expansion using the exclamation point. This is not a problem in shell scripts; only in interactive shells.

The easiest solution is unsetting the histexpand option: this can be done with set +H or set +o histexpand.

You can read more about this here on the Bash Pitfalls website.

  • 1
    This doesn't answer OP's question, it only explains an error he already solved. Commented Oct 29, 2013 at 14:17
  • 1
    @wingedsubmariner I was pointing to the wiki. I also mentioned the histexpand. The whole problem in this case is the exclamation mark (!). Commented Oct 29, 2013 at 14:20
  • Why is the error message "event not found" instead of e.g. "history expansion failed"? Commented Apr 9, 2018 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .