|
|
|
Quoting in shell scripts controls how special characters are interpreted. Understanding quoting is essential for writing correct shell scripts. |
|
|
There are three types of quoting: single quotes, double quotes, and backslash escaping. |
|
|
Double quotes preserve most characters literally but allow variable expansion and some escapes. |
|
|
Single quotes preserve ALL characters literally. No variable expansion happens inside single quotes. |
|
|
Use single quotes when you want literal text with no interpretation of special characters. |
|
|
Double quotes are needed when variables might contain spaces or special characters. |
|
|
Without quotes, the shell would split on spaces. This is called word splitting. |
|
|
The backslash escapes single characters. |
|
|
You can mix quoting styles in the same string. |
|
|
Escape sequences like \n and \t are NOT interpreted in double quotes: |
|
|
For reliable newlines, use literal newlines (shown below) or printf. Note: $‘…’ works in bash but is not POSIX. |
|