More broadly, how to echo ! in bash without space in between
karthik@cosmic:~$ echo "Hello!World"
bash: !World: event not found
karthik@cosmic:~$ echo "Hello\!World"
Hello\!World
karthik@cosmic:~$
More broadly, how to echo ! in bash without space in between
karthik@cosmic:~$ echo "Hello!World"
bash: !World: event not found
karthik@cosmic:~$ echo "Hello\!World"
Hello\!World
karthik@cosmic:~$
Thanks to someone, the solution is simple.
Remove those double quotes and escape the !:
echo Hello\!World
or with bash 4.3 or newer, use double quotes but make sure the ! is immediately followed by the closing quote:
echo "Hello!""World"
From the release notes in bash 4.3:
l. The history expansion character (!) does not cause history expansion when followed by the closing quote in a double-quoted string.
Best is to use single quotes inside which all characters lose their special meaning¹.
echo 'Hello!World'
Or disable csh-style history expansion altogether with:
histchars=
Or:
set +o histexpand
Or:
set +H
Note that history expansion is only enabled by default when bash is interactive, not in scripts.
As long as the deprecated `...` form of command substitution is not also used, as inside it, \ retains a special meaning even inside single quotes.