Bash – using ! character in echo

bashechospecial characters

I am messing with echo and having issues with echo displaying the ! character.

I tried this.

echo -e "Wake!\nUp!
bash: !\n!: event not found

Then this

# echo "Wake\!\nUp!"
Wake\!
Up!

Well now the backslash AND the exclamation mark shows. How can I use this character properly? What am I missing?

Best Answer

Use single quotes:

$ echo -e 'Wake!\nUp!'
Wake!
Up!

If you use single quotes ('') shell will treat the string literally, whereas if you use double quotes ("") it will treat ! as a reference to the previous command (event).

Related Question