I took a closer look on this phenomenon after I stumbled over it in two other questions today. I've tried all of this with the default set -H
(history expansion on).
To test a script, I often do things like echo
ing a multi-line string and pipe it through a script, but in some cases it gives an error:
$ echo "foo
bar" | sed '/foo/!d'
bash: !d': event not found
>
The !
seems to trigger history expansion, although it is enclosed with single quotes. The problem seems to be the occurrence of the double quote in the same line, because
$echo $'foo\nbar' | sed '/foo/!d'
works as well as
$echo "foo
bar" |
> sed '/foo/!d'
My suspicion: History expansion is applied linewise, so the '
after a single "
is considered to be escaped, so the following !
is not escaped.
Now my question: Is this a bug or expected behavior? Reproduced with bash
versions 4.2.30 and 4.4.12.
zsh
(history expansion on): No such problem. Really seems to be a bug inbash
– Philippos Sep 08 '17 at 07:54bash
. You can disable the whole implementation by addingset +H
to.bashrc
. In that case,bash
never attempts to do any history expansion stuff anywhere (that is, behave like proper bourne shell) and you'll have less nasty surprises. – Mikko Rantalainen Nov 14 '18 at 07:41