SEARCH

How-To Geek

What’s the Difference Between Single and Double Quotes in the Bash Shell?

Enclosing text in quotation marks is fairly standard practice on the command line, especially when dealing with files that have spaces in the names, but how do you know whether to use single or double quotes? Let’s take a look at the difference, and when you should use one vs the other.

The general rule is that double quotes still allow expansion of variables within the quotes, and single quotes don’t. Keep reading.

Quotes with Simple Text

If you’re simply enclosing a few words of text, it really doesn’t matter which one you use, as they will both work exactly the same. For instance, these two commands will both create a directory named Test Directory:

mkdir "Test Directory"
mkdir 'Test Directory'

More experienced types will probably note that you could also use mkdir Test\ Directory if you wanted.

Shell Variable Expansion

The difference between single and double quotes becomes more important when you’re dealing with variables on the command line—by default, bash is going to expand a variable like $test into the full string. First, we’ll assign the variable:

test="This is a test"

Now you can use this variable on the command line, like this, which should simply output This is a test to the console:

echo $test

Here’s where the difference between double and single quotes becomes more clear—when you use single quotes the variables aren’t going to be expanded. For instance, if you run this command:

echo '$test'

You’ll see nothing but ‘$test’ on the command line when you use single quotes, but it will output properly when you use double quotes:

image

The same thing works when you use the ` character to expand a command on the command line. For instance, say you wanted to use the pwd command from within another command—you’d use backticks to tell the shell to expand it:

echo `pwd`/test

If you were in your home folder you’d see output that looked like this:

/home/geek/test

Say, for example, you are in a folder that has a space in the path, and you want to use the ln command to create a symbolic link to a file in the current directory. You usually need to specify the full path when using the ln command, so it’s much easier to use `pwd` to save typing.

Look what happens when you try and use the command without enclosing in quotes:

ln –s `pwd`/test /home/geek/linkedname

image

Instead, you’ll need to surround in quotes:

ln –s "`pwd`/filename" /some/other/path

For a more concrete example, let’s assume that we have a folder structure like this example, where all the file names have spaces in them:

image

Since the unzip command doesn’t support using * to run it against all files, you’ll need to use the for command instead. Here’s where things get interesting:

for f in *.zip;do unzip $f;done

Oops! Looks like it didn’t work.

image

What we’ll need to do instead is use double quotes around the $f variable, like this:

for f in *.zip;do unzip "$f";done

Now every time the for command goes through the loop, it’s going to actually run a command like this one:

unzip "test 1.zip"

Of course it would loop through and run a similar command for every file in the directory.

Let’s Review

Now that we’ve gone through the examples, we’ll just quickly review in case you missed it:

Double Quotes

  • Use when you want to enclose variables or use shell expansion inside a string.
  • All characters within are interpreted as regular characters except for $ or ` which will be expanded on the shell.

Single Quotes

  • All characters within single quotes are interpreted as a string character.

And thus ends the lesson of the quotes. Use them wisely.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/17/14
  • Jacob Zinicola

    PowerShell handles single- and double-quotes similarly, though you can usually use variables as an argument directly instead of having to quote them.

Enter Your Email Here to Get Access for Free:

Go check your email!