CIS120 Linux Fundamentals by Scott Shaper

Single and Double Quotes

In Linux, quotes play a crucial role in how the shell interprets and processes text. Single quotes (' ') and double quotes (" ") are used to define strings and manage the interpretation of special characters within those strings. Understanding the differences between single and double quotes is essential for effective command-line usage.

The examples below use the course setup. Try them in ~/playground/chapter4 (run cd ~/playground/chapter4 first). The setup has created several files there, including a file named Old Name.txt (with a space) for the "creating files and folders with spaces" examples.

Single Quotes

Single quotes (' ') are used to preserve the literal value of each character within the quotes. This means that any special characters, such as $, *, or \, are treated as regular characters and not as special shell characters.

Example:

cd ~/playground/chapter4
echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'

Output:

text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER

In this example, everything inside the single quotes is treated literally. The shell does not perform any expansion or interpretation of special characters. Thus, the output is exactly what was input.

Double Quotes

Double quotes (" ") preserve the literal value of most characters within the quotes. However, they allow the interpretation of certain special characters, such as $, \, !, and backticks `.

Example:

echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"

Output (your username will appear instead of me):

text ~/*.txt {a,b} foo 4 me

In this example, the double quotes allow for variable expansion and command substitution. Here's the breakdown of what happens inside the double quotes:

The tilde (~) and brace {} expansions are not processed within double quotes.

No Quotes

When no quotes are used, the shell processes all forms of expansion: pathname expansion, brace expansion, command substitution, arithmetic expansion, and variable expansion.

Example (run from ~/playground/chapter4 so *.txt expands to the setup's .txt files):

cd ~/playground/chapter4
echo text *.txt {a,b} $(echo foo) $((2+2)) $USER

Output will look similar to (the exact list of .txt files depends on what is in the directory):

text file1.txt file2.txt file3.txt fileA.txt doc1.txt doc2.txt alpha.txt beta.txt file1a.txt file2b.txt a b foo 4 me

Here’s what happens step-by-step:

Creating Files and Folders with Spaces

When creating files or folders with spaces in their names, quotes are essential to ensure the shell correctly interprets the spaces as part of the name rather than as separators between different arguments. That being said you should not create files or folders with spaces in their names.

Examples:

From ~/playground/chapter4, the setup has created a file named Old Name.txt. You can try renaming it with mv (use quotes around names that contain spaces):

cd ~/playground/chapter4
mv "Old Name.txt" "New Name.txt"

To rename it back (or create a new file with a space for practice):

mv "New Name.txt" "Old Name.txt"

These commands create a directory or file with a space in the name (only for practice; avoid spaces in real filenames):

NOTE: The touch command will create an empty file with the given name. If the file already exists, it will be overwritten.

mkdir "My Folder"
touch "New Document.txt"