Using quotes in the Linux and MacOS command line

Quotes are often used in the command line (not only that of  Linux and MacOS, but Windows as well) as a quick way to escape white spaces so that words separated by them won’t be interpreted as multiple arguments for a command but as a single one. Consider, for example, the following snippet.
Luciano-MBA:test tech4freelancers$ mkdir test folder
Luciano-MBA:test tech4freelancers$ ls -l
total 0
drwxr-xr-x 2 tech4freelancers staff 68 Dec 22 10:42 folder
drwxr-xr-x 2 tech4freelancers staff 68 Dec 22 10:42 test

What we did by running the command mkdir test folder is creating two directories: one named test and one named folder. The mkdir command (and all other command line utilities, really) interprets white spaces as separators for multiple argument. By contrast, consider this snippet here:
Luciano-MBA:test tech4freelancers$ mkdir "test folder"
Luciano-MBA:test tech4freelancers$ ls -l
total 0
drwxr-xr-x 2 tech4freelancers staff 68 Dec 22 10:46 test folder

Putting test folder in quotes allowed us to specify that we want to create a single folder with that name. An alternative way to achieve the very same result is to put a backslash character \ before white spaces, like this.
Luciano-MBA:test tech4freelancers$ mkdir test\ folder
Luciano-MBA:test tech4freelancers$ ls -l
total 0
drwxr-xr-x 2 tech4freelancers staff 68 Dec 22 10:50 test folder

In computer parlance, this is known as using backslash as an escape character for space, or escaping white spaces. Since escaping characters is not always possible, or even practical, quoting text is by far the most widely used method telling the terminal to treat white spaces literally.

However, not everyone knows that there are actually three distinct types of quotes that the terminal recognises, each with a different behaviour. We will try to describe them in the following paragraphs.

Single quotes

Single quotes like ‘these’ are the simplest type of quotes recognized by the command line. Due to their simple behaviour, they end up being rarely used. They take everything between them and treat the whole string as a literal, without performing any additional operation. Take the following example
Luciano-MBA:/ tech4freelancers$ cd /
Luciano-MBA:/ tech4freelancers$ sudo mkdir myfolder
Luciano-MBA:/ tech4freelancers$ sudo rsync -a $HOME '/myfolder/$HOME'

This snippet creates a folder named myfolder in the root directory and then copies the content of the current user’s home directory (on MacOS, /Users/username, on Linux /home/username) inside myfolder, creating a folder literally named $HOME if necessary.

The $HOME folder inside myfolder.

There is generally no need to create files or folders that start with a $ sign. Actually, it’s even a discouraged practice, but the possibility is there if you need to.

Double quotes

Double quotes such as “these” tell the terminal to interpret a whole string literally as opposed to considering each space an argument separator, similar to what single quotes do. Contrary to single quotes, they support what is known as shell globbing (the usage of characters such as * and ? to refer to any character combination) and variable expansion. On Unix operating systems, variables can be given any name (as long as it starts with an alphabetic character) and are preceded by a $ sign. One can see a list of system variables by typing the env command. $HOME is a variable that contains the path to the current user’s home folder. They’re the most-commonly used type of quotes thanks to their support for variable expansion and shell globbing. Take this example:

Luciano-MBA:/ tech4freelancers$ echo 'The current user is $LOGNAME'
The current user is $LOGNAME
Luciano-MBA:/ tech4freelancers$ echo "The current user is $LOGNAME"
The current user is tech4freelancers

As you can see, the variable $LOGNAME inside double quotes is processed as such, whereas that inside single quotes is treated literally.

Back-quotes or tick-quotes

Back-quotes like `these` really are the same characters as grave accents without a letter below them. They work similarly to double quotes in that they support variable expansion and shell globbing. The similarities end there, however. Back quotes aren’t “real” quotes in that they expect to enclose a command.

Most keyboard layouts do not have a key dedicated to write back-quotes. The reason for this may be traced to the fact that Windows, by far the most ubiquitous operating system, does not support back-quotes. On MacOS, it’s possible to write them by using the keyboard combination Alt + 9 followed by a space. Linux users can find out how to write that character by going to System Preferences > Keyboard.

Luciano-MBA:/ tech4freelancers$ echo `The current user is $LOGNAME`
-bash: The: command not found
Luciano-MBA:/ tech4freelancers$ echo The current user is `echo $LOGNAME`
The current user is tech4freelancers

The whole string is not treated as a literal, though, as shown in the example below:

Luciano-MBA:/ tech4freelancers$ cd /
Luciano-MBA:/ tech4freelancers$ sudo mkdir myfolder
Luciano-MBA:/ tech4freelancers$ cd myfolder
Luciano-MBA:myfolder tech4freelancers$ sudo mkdir -v `date`
mkdir: created directory 'Thu'
mkdir: created directory 'Dec'
mkdir: created directory '22'
mkdir: created directory '17:14:34'
mkdir: created directory 'CET'
mkdir: created directory '2016'
Luciano-MBA:myfolder tech4freelancers$ ls
17:14:34 22 Dec
2016 CET Thu

Whoops, instead of creating a folder named after the current date and time, we created multiple folders!

Combining quote types

Since back-quotes are an odd beast, not only it is possible, but it’s also required to combine them with double quotes in order to make them behave the way you want them to expect. Keeping with the example above:

Luciano-MBA:/ tech4freelancers$ cd /
Luciano-MBA:/ tech4freelancers$ sudo mkdir myfolder
Luciano-MBA:/ tech4freelancers$ cd myfolder
Luciano-MBA:myfolder tech4freelancers$ sudo mkdir -v "Backup `date`"
mkdir: created directory 'Backup Thu Dec 22 17:25:58 CET 2016'

The combination of double quotes and back-quotes allowed us to create a directory named Backup + current date and time. Really nifty, huh?

About Andrea Luciano Damico 109 Articles
Andrea Luciano Damico is a freelance translator from Italy. Among his interests are linguistics, technology, video games, and generally being a chill guy. He runs Let's Translate.it and Tech4Freelancers.net.