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 \<
<

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Bash,
- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
More Posts:
- Delete blank lines in a file using Notepad++ - NotepadPlusPlus
- How to find Integer MAX and MIN Value in Java - Java
- How to create a SharePoint list from an existing list with same columns, views, formatting, data - SharePoint
- How to install Python 3.11 on Mac - Python
- Program 1: Print Hello World! - 1000+ Python Programs - Python-Programs
- ls Command to See Hidden Files - Linux
- [Fix] Linux - bash: useradd: command not found - Linux
- How to Connect to Azure AD Account using PowerShell on Mac - Powershell