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

what-and-8217;s-the-difference-between-single-and-double-quotes-in-the-bash-shell photo 1

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.

what-and-8217;s-the-difference-between-single-and-double-quotes-in-the-bash-shell photo 2

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:

what-and-8217;s-the-difference-between-single-and-double-quotes-in-the-bash-shell photo 3

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

what-and-8217;s-the-difference-between-single-and-double-quotes-in-the-bash-shell photo 4

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:

what-and-8217;s-the-difference-between-single-and-double-quotes-in-the-bash-shell photo 5

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.

what-and-8217;s-the-difference-between-single-and-double-quotes-in-the-bash-shell photo 6

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.

Article What’s the Difference Between Single and Double Quotes in the Bash Shell? compiled by Original article here

More stories

How to See What’s Eating Up Your Android Device’s Storage

It’s all too easy to stuff your Android phone or tablet to the gills with data and find yourself without room to install new apps or download new media. Today we’re taking a look at how to quickly assess what’s eating up all your disk space.

How to Recreate the Channel Surfing Experience on XBMC

Running a home media server is pretty awesome: beautiful cover art, playlists, and all your media right at your finger tips. One thing media centers fall short on, however, is recreating that feeling of aimlessly channel surfing just to scratch that TV watching itch. Read on as we show you how to