If you try to echo a String as a Bash command or a Script that has a special character such as an exclamation mark, you will see that it will not work as intended or you get an error.
Example:bash-3.2$ echo Hello World!
bash: !": event not found
Below is an extensive table that enlists the special characters and how to escape them with Bash String or Script.
| Special Character | Meaning | How to Escape |
|---|---|---|
" |
Double quote | \" |
' |
Single quote | \' |
$ |
Dollar sign | \$ |
& |
Ampersand | \& |
( |
Left parenthesis | \( or \) |
) |
Right parenthesis | \( or \) |
{ |
Left curly brace | \{ |
} |
Right curly brace | \} |
[ |
Left square bracket | \[ |
] |
Right square bracket | \] |
! |
Exclamation mark | \! |
* |
Asterisk (wildcard) | \* |
? |
Question mark (wildcard) | \? |
\ |
Backslash | \\ |
> |
Greater than sign (output redirection) | \> |
< |
Less than sign (input redirection) | \< |
; |
Semicolon (command separator) | \; |
Now let's make our "Hello World!" string work.
$ echo Hello World \!
Hello World
Example:
# echo <
bash: syntax error near unexpected token `newline'
Fix:
# echo \<
<

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!