I recently came across this surprising behavior involving "
and !
in bash:
guy@guydell:~$ echo arbitrary echo
arbitrary echo
guy@guydell:~$ echo "!e" #unescaped history expansion
echo "echo arbitrary echo" #unescaped history expansion
echo arbitrary echo
guy@guydell:~$ echo "!e" #escaped history expansion
!e
Notice that in the second to last line, history expansion does not occur. This is because I used a to escape the
!
. However, the remains in the input to echo. This is not the case with e.g.
"
:
guy@guydell:~$ echo """
"
This behavior is documented:
If enabled, history expansion will be performed unless an ‘!’ appearing in double quotes is escaped using a backslash. The backslash preceding the ‘!’ is not removed.
I was curious as to what motivated this design decision. I would expect that, if a were to escape a character in a string of text, the
would not be included in the string. Directions to an archive of an email list (which is where I assume this would have been discussed) or an excerpt from the discussion would both be appreciated.
5