[Techtalk] bash and '!?'

Kathryn Hogg kjh at flyballdogs.com
Fri Sep 18 15:36:34 UTC 2015


On 2015-09-18 10:25, Miriam English wrote:
> Does anybody have any idea why this happens?
> 
> I illustrate using the echo command here, but the same thing happens
> with other commands, so it's something about bash, not any particular
> command. I precede my commands with "$" to distinguish them from
> results.
> 
> 
> $ echo "what?"
> what?
> $ echo "what!"
> what!
> $ echo "what?!"
> what?!
> $ echo "what! ?"
> what! ?
> $ echo "what!?"
> echo "whatecho "what? !"
>> 
> 
> The "!?" combination seems to insert the previous command in the
> command history into the current command, deleting the rest of the
> current line, but I'm not sure of this because sometimes other weird
> things happen.
> 
> Can anybody explain it? Is it a bug, or is it a sensible thing?

!? is part of history expansion  and is expanded inside double quotes 
just like shell variables are ($var).

If you want to turn off expansions, simply enclose your string in single 
quotes instead of double quotes:

$ echo 'what!?'
what!?

Or you can use a \ before the history expansion character"

$ echo "what\!?"
what!?

The bash(1) manual has a section named HISTORY EXPANSION that explains 
the gritty details.


-- 
Kathryn
http://womensfooty.com




More information about the Techtalk mailing list