Single quotes are literal
Everything between a pair of single quotes is taken literally. Dollar signs, backslashes, backticks, asterisks, and spaces all lose their special meaning. A single quote cannot appear inside single quotes, even when escaped with a backslash, because the backslash itself is literal there. That single limit is the reason the ANSI-C form and the quoting trick in the escaping lesson exist. Use single quotes for any text that must reach a program unchanged, such as a regular expression, an awk program, or a string that contains dollar signs.
echo '$HOME is literal'
echo '*.txt stays as typed'$HOME is literal
*.txt stays as typedDouble quotes keep most expansions
Double quotes stop word splitting and filename expansion, but they still allow parameter expansion, command substitution, and arithmetic expansion. Inside double quotes, a backslash only has special meaning before a dollar sign, a backtick, a double quote, another backslash, or a newline. Any other backslash is kept. A single quote inside double quotes is an ordinary character. Double quotes are therefore the default for any argument that holds a value, which is why the advice to quote variables appears in almost every shell guide.
name='Ada Lovelace'
echo "Hello, $name"
echo "A single ' is fine here"
echo "a\tb stays as a backslash and t"Hello, Ada Lovelace
A single ' is fine here
a\tb stays as a backslash and tInteractive bash has one more hazard inside double quotes. History expansion can rewrite text that contains an exclamation mark before the command runs, so a password-like string with an exclamation mark may need single quotes when you type it at the prompt. Scripts do not perform history expansion, so the problem appears mainly at the keyboard.
The ANSI-C form
The form $'...' is a bash extension, sometimes called ANSI-C quoting. Inside it, backslash escapes are processed in the same way as in C. The common ones are \t for tab, \n for newline, \\ for a backslash, and \' for a single quote. Hexadecimal escapes such as \x41 and Unicode escapes such as \u00e9 are also accepted. This form is the easiest way to put a tab or newline into an argument, and it is also the answer to the question of how to include a single quote in a string that you want to write with single quotes.
echo $'col1\tcol2'
echo $'it\'s one line\nand a second'col1 col2
it's one line
and a secondThe first line contains a real tab character between the two columns. The second line shows that the escaped quote and the newline are both handled by the ANSI-C rules.
Adjacent pieces form one word
Quoting styles can be mixed in one word without spaces. The pieces are joined, and quote removal takes away each set of quote characters. The result is one argument built from literal text, expanded text, and escaped characters. This is convenient for building a single argument from several parts, and it is also the reason some quoting bugs are hard to see. Read the word from left to right, and mark each quote pair as you go.
v=mid
echo 'start-'"$v"$'\tend'start-mid endThe locale form and when to use it
The form $"..." asks bash to translate the string through the message catalog for the current locale. Scripts that ship translations use it, and without a catalog it behaves exactly like double quotes. You will rarely write it yourself, but it is useful to recognize it when you read other people's scripts.
Mixing quotes in a real command
Most real commands mix styles, and reading them is a skill of its own. Take a line that searches a log for a value held in a variable, where the log name contains a space. The search pattern needs double quotes so the variable is filled in. The file name needs quotes so the space stays inside one argument. Fixed options such as -n need no quotes at all. Reading the line piece by piece, each quoted region protects exactly what it encloses, and the unquoted pieces are plain words that bash will split at spaces.
code=404
grep -n "status $code" 'access log.txt'Writing the line this way keeps the file name and the pattern separate, so a change to one does not affect the other. It is the same discipline that the splitting and globbing lessons described, applied to every argument in the command.
Quoting inside awk and other programs
Single quotes are the normal choice for programs that have their own dollar signs, such as an awk script. In awk '{ print $1 }', the single quotes stop bash from expanding $1, so awk receives the field reference it expects. If the same program were written in double quotes, bash would try to expand $1 as a positional parameter, which is almost never what you intend. When the program needs a value from a shell variable, close the single quotes around the program and insert the variable with double quotes, a pattern you will see in many real scripts.
Choosing a quote style
A simple rule covers most cases. Use single quotes for fixed text that contains special characters, double quotes for any value you need expanded, and the ANSI-C form only when you need an escape such as a tab or newline. When you are unsure, quote with double quotes and read the result with printf '[%s]\n', which shows exactly where the argument begins and ends. The lab in this lesson asks you to produce three values that each use one of these forms.