I just now found another way, that will at least work with echo
ing strings (sentences) you want to punctuate with an exclamation point. It does an end-run, more or less, around Bash histexpand and takes only a bit longer to code.
The hex for an exclamation point, as listed on
http://www.ascii-code.com/, is 21, so if you put \x21
at the end of your string, echo -e $foo
, make $foo
its own expanded echo [ie, foo=$(echo -e "$foo")
], what you get when you echo $foo
again is the string with an !
at the end. And no switching histexpand either.
Works for sure in Bash 4+. Earlier versions, ymmv.
1As I noted before the command you put does actually work on it's own :)
bzr commit -m "It works!"
– h4unt3r – 2014-08-18T03:26:53.407An even quirkier case with nested quotes: http://stackoverflow.com/questions/22125658/how-to-escape-history-expansion-exclamation-mark-inside-a-double-quoted-comm
– Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-09-02T18:08:33.3701
Though the accepted answer is a good workaround for your commit problems, I feel it's not an answer to the actual issue: Bash history expansion. Please consider accepting Dennis' answer?
– Arjan – 2015-11-29T10:19:08.510@h4unt3r Doesn't appear to work for me?
echo "It works!" -bash: !": event not found
– rogerdpack – 2020-02-10T18:24:13.2731Doing
bzr commit -m "It works"!
works, too. – kba – 2013-05-18T13:59:17.493