h1

Shell Tips 2: Quote Unquote

February 14, 2011

There are three different kinds of quote: single quotes, double quote, and backticks. Single quotes

''

are true quotes and suspend all special characters except ‘ itself.

Double quote
"" suspend execution and filename expansion, but shell variables are still expanded.
Illustrating filename expansion:

MyList=*

will put a list of files in the variable $MyList, whereas

MyList="*"

will store an asterisk character in it.
If you just want to quote a common-or-garden string, always use single quotes.
Are double quotes useful? Very! If you double-quote all your variables (except
on the left of an assignment)…

"$LikeThis"

you can avoid a lot of problems.
Double quotes are useful in combination with backticks or or $() operator. The point of this gizmo is to take the
output of a command and turn it into a string. If a command produces output on multiple line, the
formatting gets lost. For instance

OutPut1=$(ls)

will store a list of files all in one line:

echo $OutPut1

produces

aFile anotherFile

whereas

OutPut2="$(ls)"

will maintain the formatting:

echo "$OutPut2"

produces

aFile
anotherFile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: