5

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?

6

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).

  • I figured using ( ' ' ) would take the ( \ ) literally also. I guess the -e takes care of taking escape sequences literally. – Michael Bruce Mar 21 '15 at 20:20
  • 1
    @AcousticBruce: You can think of this as two steps..at first we have escaped the shell by using single quotes, by doing this we have solved the problem regarding !.. as we got the string intact Wake!\nUp!, it is passed to echo ..again we need to get a new line by \n ..to do so we need the -e option of echo so that it will enable back slash escapes and we will get the new line... – heemayl Mar 21 '15 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.