double quote of variable in test or [ ] or [[ ]]

Posted on 2011-10-09
Last Modified: 2013-12-26
Hello, I am trying to follow on some bash scripting tutorial and realized that we need to be careful on using a variable with or without double quote in test/[]/[[]] operator. But I am getting some confusion when and where do we use that. Could someone please help me to understand it with example. Thanks!
Question by:beer9
    LVL 48

    Accepted Solution

    Hopefully the following example makes it clearer

    foo="some value"
    [ $foo = "some value" ] && echo test1
    [ "$foo" = "some value" ] && echo test2
    [[ $foo = "some value" ]] && echo test3

    Open in new window

    Output is: line 3: [: too many arguments

    Open in new window

    Short answer is that you should use quotes around variables when you use [, but is not needed when you use [[
    LVL 31

    Assisted Solution

    The newer standard is to use [[ ]] in both Bash and Korn.  [ ] is for backward compatibility with Borne shell.

    With [[ ]], you can use all the C++ like operator like ==, <=, etc.
    With [[ ]], you can use the latest Bash regular expression matching as well.
    All the new power of of Bash obtained by using [[ ]].  But some people still want to keep using [ ].

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now