Unix How-To: Quotes to Live By

By Sandra Henry-Stocker  5 comments

One of the things that really throws new Unix users is when to use quotes and which kind of quotes. Between single quotes, double quotes and back ticks, a person can start to feel that Unix is unnecessarily tricky.

Double quotes are the easiest to understand. Unlike Windows, the only way to work on the command line with files whose names contain blanks (e.g., "my file") is to enclose the file names in some kind of quotes. If you want to echo text to a user containing strings of consecutive blanks (e.g., "first second"), you need the quotes, else you get "first second".

You might also use double quotes to keep apostrophes (i.e., single quotes) from being interpreted by the shell as the beginning of strings. If you type a phrase containing an apostrophe, Unix will keep looking for the matching end of quote and will prompt with its secondary prompt (usually a ">"):

$ echo What's wrong now?
>

Of course, you can always "escape" the apostrophe so that the shell doesn't react to it as if it were a delimiter. But a user who can anticipate the need for an escape is probably one who will know when to enclose strings in double quotes in the first place.

$ echo what\'s wrong now?
what's wrong now?

Single quotes work the same as double quotes except for one very significant difference. Double quotes allow variables within to be resolved; single quotes do not. So, echo "Hello, $USER" becomes Hello, jdoe, while echo 'Hello, $USER' displays Hello, $USER. If you want to redirect a statement such as Hello, $USER to a script, it makes a big difference which form you use.

One common use of both double and single quotes is to prevent a variable, which just might have a value with embedded quotes, from disrupting an if statement. The code below looks innocuous enough, but if the variable $var2 gets a value of "10 days" instead of "10", the if statement breaks.

if [ $var2 == 10 ]; then

Putting quotes around $var2 would prevent this kind of thing from happening.

if [ '$var2' == 10 ]; then

Back ticks have an altogether different purpose altogether. They are used to invoke a command, replacing the command in a line of text with the command's output. You can type:

$ echo Today is `date +%A`

The output will be of the form "Today is Wednesday".

Even an experienced Unix user will trip over quote problems now and then. But echo "That's Life!".

5 comments

    Anonymous 1 year ago
    Put "problem characters" into variables...$ bang='!'$ echo "That's Life${bang}"That's Life!
    Anonymous 1 year ago
    There is another good reason for quoting variables when testing for a value. If the variable has no value, and the variable is not quoted, an error will be generated.MYVAR='';# this line fails[ $MYVAR == 'testing' ] && echo this is a test# this line works[ "$MYVAR" == 'testing' ] && echo this is a test
    Anonymous 1 year ago
    I think the example in the article,
    if [ '$var2' == 10 ]; then
    should have been:
    if [ "$var2" == 10 ]; then
    Anonymous 1 year ago
    In the example "if [ '$var2' == 10 ]; then", I think those should have been double-quotes around $var2.
    Anonymous 1 year ago
    > Unlike Windows, the only way to work on the command line> with files whose names contain blanks (e.g., "my file")> is to enclose the file names in some kind of quotes.Not a knock on the article but quotes aren't the only way to deal with spaces on the command line. You can escape the spaces, too. e.g. $ This is a pain.txtEscaping the spaces is more of a pain, as you can see, than using quotes but it's nice to have options.

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Operating SystemsWhite Papers & Webcasts

      White Paper

      Microsoft Enterprise Agreement Program Overview

      Discover how flexible the Microsoft Enterprise Agreement Program is to help you build the right software solution agreement for your business. This paper highlights all the available options-from on-premise software and cloud service solutions, to payment options and enrollment programs, and more.

      White Paper

      Watson - A System Designed for Answers. The future of workload optimized systems design

      Watson is a workload optimized system designed for complex analytics, made possible by integrating massively parallel POWER7 processors and DeepQA technology. Read the white paper about Watson's workload optimized system design.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question