Single versus double quotes in BASH

by
Ryan
on
October 15, 2008

Understanding the difference between double versus single quotes is important when using BASH. Many times you may have seen them being used interchangeably. The basic difference is that variable names will be expanded within double quotes but not within single ones.

This example shows the normal output with no quotes.

$ echo $USER
ryan

As you can see the shell will expand the variable.

In this example we will surround the variable with double quotes.

$ echo "$USER"
ryan

We see the same result as before.

This time with single quotes.

$ echo '$USER'
$USER

The variable name is not expanded.

Here is another interesting case. The single quotes are inside the double quotes.

$ echo "'$USER'"
'ryan'

The variable is expanded and the single quotes are retained.

Here I flip the order with the single quotes on the outside.

$ echo '"$USER"'
"$USER"

The variable is not expanded but the double quotes are retained.

1 Comment
bash
, , , ,

No related posts.

Comments (1)

A couple more interesting cases:

echo \”$USER’\’
‘$USER’

echo “‘”‘$USER’”‘”
‘$USER’

Trackbacks (0)

No trackbacks yet

Leave a Comment

(displayed with your post)
(will not be published)
(optional)
Copyright 2008-2010 WiredRevolution.com. All rights reserved.