Difference between [ and [[ in if statements [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • What is the difference between [[ $a == z* ]] and [ $a == z* ]?

    3 answers



  • Why does parameter expansion with spaces without quotes work inside double brackets “[[” but not inside single brackets “[”?

    4 answers



The following code



if [ $a == "apple" ];
then
echo "True"
else
echo "False"
fi


outputs "True" ("False") if a="apple" (a="plum"). The comparison fails if one uses wildcards:



if [ $a == "appl"* ];


and is fixed if one replaces [ by [[:



if [[ $a == "appl"* ]];


What is the difference between [ and [[ in if statements?







share|improve this question












marked as duplicate by don_crissti, ilkkachu, Community♦ Dec 23 '17 at 16:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    0
    down vote

    favorite













    This question already has an answer here:



    • What is the difference between [[ $a == z* ]] and [ $a == z* ]?

      3 answers



    • Why does parameter expansion with spaces without quotes work inside double brackets “[[” but not inside single brackets “[”?

      4 answers



    The following code



    if [ $a == "apple" ];
    then
    echo "True"
    else
    echo "False"
    fi


    outputs "True" ("False") if a="apple" (a="plum"). The comparison fails if one uses wildcards:



    if [ $a == "appl"* ];


    and is fixed if one replaces [ by [[:



    if [[ $a == "appl"* ]];


    What is the difference between [ and [[ in if statements?







    share|improve this question












    marked as duplicate by don_crissti, ilkkachu, Community♦ Dec 23 '17 at 16:11


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:



      • What is the difference between [[ $a == z* ]] and [ $a == z* ]?

        3 answers



      • Why does parameter expansion with spaces without quotes work inside double brackets “[[” but not inside single brackets “[”?

        4 answers



      The following code



      if [ $a == "apple" ];
      then
      echo "True"
      else
      echo "False"
      fi


      outputs "True" ("False") if a="apple" (a="plum"). The comparison fails if one uses wildcards:



      if [ $a == "appl"* ];


      and is fixed if one replaces [ by [[:



      if [[ $a == "appl"* ]];


      What is the difference between [ and [[ in if statements?







      share|improve this question













      This question already has an answer here:



      • What is the difference between [[ $a == z* ]] and [ $a == z* ]?

        3 answers



      • Why does parameter expansion with spaces without quotes work inside double brackets “[[” but not inside single brackets “[”?

        4 answers



      The following code



      if [ $a == "apple" ];
      then
      echo "True"
      else
      echo "False"
      fi


      outputs "True" ("False") if a="apple" (a="plum"). The comparison fails if one uses wildcards:



      if [ $a == "appl"* ];


      and is fixed if one replaces [ by [[:



      if [[ $a == "appl"* ]];


      What is the difference between [ and [[ in if statements?





      This question already has an answer here:



      • What is the difference between [[ $a == z* ]] and [ $a == z* ]?

        3 answers



      • Why does parameter expansion with spaces without quotes work inside double brackets “[[” but not inside single brackets “[”?

        4 answers









      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 23 '17 at 15:52









      Viesturs

      224138




      224138




      marked as duplicate by don_crissti, ilkkachu, Community♦ Dec 23 '17 at 16:11


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by don_crissti, ilkkachu, Community♦ Dec 23 '17 at 16:11


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted











          • [ is a command (basically a variant of the test command). [[ is a builtin in many shells.

          • When you write foo* inside [...] filename expansion (aka globbing) occurs; while inside [[...]] pattern matching occurs.


          • [[ is more powerful and capable than [ but should not be used if portability is a concern.


          • [ and [[ are not part of the if syntax, you can use them as in [ "$exit" = 'yes' ] && exit.

          • Inside [...] you should prefer = instead of ==. As far as I know, the second one is accepted in many shells but is not POSIX-compliant.

          By the way, I recommend you to double-quote your variables even if you really know how word splitting will behave.






          share|improve this answer





























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted











            • [ is a command (basically a variant of the test command). [[ is a builtin in many shells.

            • When you write foo* inside [...] filename expansion (aka globbing) occurs; while inside [[...]] pattern matching occurs.


            • [[ is more powerful and capable than [ but should not be used if portability is a concern.


            • [ and [[ are not part of the if syntax, you can use them as in [ "$exit" = 'yes' ] && exit.

            • Inside [...] you should prefer = instead of ==. As far as I know, the second one is accepted in many shells but is not POSIX-compliant.

            By the way, I recommend you to double-quote your variables even if you really know how word splitting will behave.






            share|improve this answer


























              up vote
              2
              down vote



              accepted











              • [ is a command (basically a variant of the test command). [[ is a builtin in many shells.

              • When you write foo* inside [...] filename expansion (aka globbing) occurs; while inside [[...]] pattern matching occurs.


              • [[ is more powerful and capable than [ but should not be used if portability is a concern.


              • [ and [[ are not part of the if syntax, you can use them as in [ "$exit" = 'yes' ] && exit.

              • Inside [...] you should prefer = instead of ==. As far as I know, the second one is accepted in many shells but is not POSIX-compliant.

              By the way, I recommend you to double-quote your variables even if you really know how word splitting will behave.






              share|improve this answer
























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted







                • [ is a command (basically a variant of the test command). [[ is a builtin in many shells.

                • When you write foo* inside [...] filename expansion (aka globbing) occurs; while inside [[...]] pattern matching occurs.


                • [[ is more powerful and capable than [ but should not be used if portability is a concern.


                • [ and [[ are not part of the if syntax, you can use them as in [ "$exit" = 'yes' ] && exit.

                • Inside [...] you should prefer = instead of ==. As far as I know, the second one is accepted in many shells but is not POSIX-compliant.

                By the way, I recommend you to double-quote your variables even if you really know how word splitting will behave.






                share|improve this answer















                • [ is a command (basically a variant of the test command). [[ is a builtin in many shells.

                • When you write foo* inside [...] filename expansion (aka globbing) occurs; while inside [[...]] pattern matching occurs.


                • [[ is more powerful and capable than [ but should not be used if portability is a concern.


                • [ and [[ are not part of the if syntax, you can use them as in [ "$exit" = 'yes' ] && exit.

                • Inside [...] you should prefer = instead of ==. As far as I know, the second one is accepted in many shells but is not POSIX-compliant.

                By the way, I recommend you to double-quote your variables even if you really know how word splitting will behave.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 23 '17 at 16:28

























                answered Dec 23 '17 at 16:10









                nxnev

                2,4622423




                2,4622423












                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay