bash - case-insensitive matching of variable

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











up vote
3
down vote

favorite












This syntax prints "linux" when variable equals "no":



 [[ $LINUX_CONF = no ]] && echo "linux"


How would I use regular expressions (or similar) in order to make the comparison case insensitive?










share|improve this question



























    up vote
    3
    down vote

    favorite












    This syntax prints "linux" when variable equals "no":



     [[ $LINUX_CONF = no ]] && echo "linux"


    How would I use regular expressions (or similar) in order to make the comparison case insensitive?










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      This syntax prints "linux" when variable equals "no":



       [[ $LINUX_CONF = no ]] && echo "linux"


      How would I use regular expressions (or similar) in order to make the comparison case insensitive?










      share|improve this question















      This syntax prints "linux" when variable equals "no":



       [[ $LINUX_CONF = no ]] && echo "linux"


      How would I use regular expressions (or similar) in order to make the comparison case insensitive?







      linux bash regular-expression case-sensitivity






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 2 '17 at 20:14









      nath

      633421




      633421










      asked Oct 2 '17 at 16:09









      yael

      2,0361145




      2,0361145




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          No need to use that ksh-style [[...]] command, you can use the standard sh case construct here:



          case $LINUX_CONF in
          ([Nn][Oo]) echo linux;;
          (*) echo not linux;;
          esac


          Or naming each possible case individually:



          case $LINUX_CONF in
          (No | nO | NO | no) echo linux;;
          (*) echo not linux;;
          esac


          For a bash-specific way to do case-insensitive matching, you can do:



          shopt -s nocasematch
          [[ $LINUX_CONF = no ]] && echo linux


          Or:



          [[ $LINUX_CONF,, = no ]] && echo linux


          (where $VAR,, is the syntax to convert a string to lower case).



          You can also force a variable to be converted to lowercase upon assignment with:



          typeset -l LINUX_CONF


          That also comes from ksh and is also supported by bash and zsh.



          More variants with other shells:



          zsh



          set -o nocasematch
          [[ $LINUX_CONF =~ no ]] && echo linux


          (same as in bash).



          setopt extendedglob
          [[ $LINUX_CONF = (#i)no ]] && echo linux


          (less dangerous than making all matches case insensitive)



          [[ $(L)LINUX_CONF = no ]] && echo linux


          (convert to lowercase operator)



          set -o rematchpcre
          [[ $LINUX_CONF =~ (?i)no ]]


          (PCRE syntax)



          ksh93



          [[ $LINUX_CONF = ~(i)no ]]


          or



          [[ $LINUX_CONF = ~(i:no) ]]


          Note that all approaches above other than [nN][oO] to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.



          In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i and I letters and whether the dot is there or not on the uppercase or lowercase version.



          What that means is that [[ $VAR,, = oui ]] is not guaranteed to match on OUI in every locale (even when the bug in current versions of bash is fixed).






          share|improve this answer






















          • ok fine but how we do the same in if syntax?
            – yael
            Oct 2 '17 at 16:13










          • @yael, why would you want to use if? If that's for the else part, see edit.
            – Stéphane Chazelas
            Oct 2 '17 at 16:15










          • There is also [[ "$LINUX_CONF" =~ [Nn][Oo] ]] (what with the OP specifically asking for a RE).
            – DopeGhoti
            Oct 2 '17 at 16:21







          • 1




            @DopeGhoti, or [[ $LINUX_CONF = [Nn][Oo] ]] but that has little advantage over the standard case syntax.
            – Stéphane Chazelas
            Oct 2 '17 at 16:22










          • In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih $var,,.
            – DopeGhoti
            Oct 2 '17 at 16:23

















          up vote
          0
          down vote













          Keep your existing command but on the line before it run this:



          LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')


          Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395685%2fbash-case-insensitive-matching-of-variable%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            8
            down vote



            accepted










            No need to use that ksh-style [[...]] command, you can use the standard sh case construct here:



            case $LINUX_CONF in
            ([Nn][Oo]) echo linux;;
            (*) echo not linux;;
            esac


            Or naming each possible case individually:



            case $LINUX_CONF in
            (No | nO | NO | no) echo linux;;
            (*) echo not linux;;
            esac


            For a bash-specific way to do case-insensitive matching, you can do:



            shopt -s nocasematch
            [[ $LINUX_CONF = no ]] && echo linux


            Or:



            [[ $LINUX_CONF,, = no ]] && echo linux


            (where $VAR,, is the syntax to convert a string to lower case).



            You can also force a variable to be converted to lowercase upon assignment with:



            typeset -l LINUX_CONF


            That also comes from ksh and is also supported by bash and zsh.



            More variants with other shells:



            zsh



            set -o nocasematch
            [[ $LINUX_CONF =~ no ]] && echo linux


            (same as in bash).



            setopt extendedglob
            [[ $LINUX_CONF = (#i)no ]] && echo linux


            (less dangerous than making all matches case insensitive)



            [[ $(L)LINUX_CONF = no ]] && echo linux


            (convert to lowercase operator)



            set -o rematchpcre
            [[ $LINUX_CONF =~ (?i)no ]]


            (PCRE syntax)



            ksh93



            [[ $LINUX_CONF = ~(i)no ]]


            or



            [[ $LINUX_CONF = ~(i:no) ]]


            Note that all approaches above other than [nN][oO] to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.



            In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i and I letters and whether the dot is there or not on the uppercase or lowercase version.



            What that means is that [[ $VAR,, = oui ]] is not guaranteed to match on OUI in every locale (even when the bug in current versions of bash is fixed).






            share|improve this answer






















            • ok fine but how we do the same in if syntax?
              – yael
              Oct 2 '17 at 16:13










            • @yael, why would you want to use if? If that's for the else part, see edit.
              – Stéphane Chazelas
              Oct 2 '17 at 16:15










            • There is also [[ "$LINUX_CONF" =~ [Nn][Oo] ]] (what with the OP specifically asking for a RE).
              – DopeGhoti
              Oct 2 '17 at 16:21







            • 1




              @DopeGhoti, or [[ $LINUX_CONF = [Nn][Oo] ]] but that has little advantage over the standard case syntax.
              – Stéphane Chazelas
              Oct 2 '17 at 16:22










            • In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih $var,,.
              – DopeGhoti
              Oct 2 '17 at 16:23














            up vote
            8
            down vote



            accepted










            No need to use that ksh-style [[...]] command, you can use the standard sh case construct here:



            case $LINUX_CONF in
            ([Nn][Oo]) echo linux;;
            (*) echo not linux;;
            esac


            Or naming each possible case individually:



            case $LINUX_CONF in
            (No | nO | NO | no) echo linux;;
            (*) echo not linux;;
            esac


            For a bash-specific way to do case-insensitive matching, you can do:



            shopt -s nocasematch
            [[ $LINUX_CONF = no ]] && echo linux


            Or:



            [[ $LINUX_CONF,, = no ]] && echo linux


            (where $VAR,, is the syntax to convert a string to lower case).



            You can also force a variable to be converted to lowercase upon assignment with:



            typeset -l LINUX_CONF


            That also comes from ksh and is also supported by bash and zsh.



            More variants with other shells:



            zsh



            set -o nocasematch
            [[ $LINUX_CONF =~ no ]] && echo linux


            (same as in bash).



            setopt extendedglob
            [[ $LINUX_CONF = (#i)no ]] && echo linux


            (less dangerous than making all matches case insensitive)



            [[ $(L)LINUX_CONF = no ]] && echo linux


            (convert to lowercase operator)



            set -o rematchpcre
            [[ $LINUX_CONF =~ (?i)no ]]


            (PCRE syntax)



            ksh93



            [[ $LINUX_CONF = ~(i)no ]]


            or



            [[ $LINUX_CONF = ~(i:no) ]]


            Note that all approaches above other than [nN][oO] to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.



            In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i and I letters and whether the dot is there or not on the uppercase or lowercase version.



            What that means is that [[ $VAR,, = oui ]] is not guaranteed to match on OUI in every locale (even when the bug in current versions of bash is fixed).






            share|improve this answer






















            • ok fine but how we do the same in if syntax?
              – yael
              Oct 2 '17 at 16:13










            • @yael, why would you want to use if? If that's for the else part, see edit.
              – Stéphane Chazelas
              Oct 2 '17 at 16:15










            • There is also [[ "$LINUX_CONF" =~ [Nn][Oo] ]] (what with the OP specifically asking for a RE).
              – DopeGhoti
              Oct 2 '17 at 16:21







            • 1




              @DopeGhoti, or [[ $LINUX_CONF = [Nn][Oo] ]] but that has little advantage over the standard case syntax.
              – Stéphane Chazelas
              Oct 2 '17 at 16:22










            • In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih $var,,.
              – DopeGhoti
              Oct 2 '17 at 16:23












            up vote
            8
            down vote



            accepted







            up vote
            8
            down vote



            accepted






            No need to use that ksh-style [[...]] command, you can use the standard sh case construct here:



            case $LINUX_CONF in
            ([Nn][Oo]) echo linux;;
            (*) echo not linux;;
            esac


            Or naming each possible case individually:



            case $LINUX_CONF in
            (No | nO | NO | no) echo linux;;
            (*) echo not linux;;
            esac


            For a bash-specific way to do case-insensitive matching, you can do:



            shopt -s nocasematch
            [[ $LINUX_CONF = no ]] && echo linux


            Or:



            [[ $LINUX_CONF,, = no ]] && echo linux


            (where $VAR,, is the syntax to convert a string to lower case).



            You can also force a variable to be converted to lowercase upon assignment with:



            typeset -l LINUX_CONF


            That also comes from ksh and is also supported by bash and zsh.



            More variants with other shells:



            zsh



            set -o nocasematch
            [[ $LINUX_CONF =~ no ]] && echo linux


            (same as in bash).



            setopt extendedglob
            [[ $LINUX_CONF = (#i)no ]] && echo linux


            (less dangerous than making all matches case insensitive)



            [[ $(L)LINUX_CONF = no ]] && echo linux


            (convert to lowercase operator)



            set -o rematchpcre
            [[ $LINUX_CONF =~ (?i)no ]]


            (PCRE syntax)



            ksh93



            [[ $LINUX_CONF = ~(i)no ]]


            or



            [[ $LINUX_CONF = ~(i:no) ]]


            Note that all approaches above other than [nN][oO] to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.



            In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i and I letters and whether the dot is there or not on the uppercase or lowercase version.



            What that means is that [[ $VAR,, = oui ]] is not guaranteed to match on OUI in every locale (even when the bug in current versions of bash is fixed).






            share|improve this answer














            No need to use that ksh-style [[...]] command, you can use the standard sh case construct here:



            case $LINUX_CONF in
            ([Nn][Oo]) echo linux;;
            (*) echo not linux;;
            esac


            Or naming each possible case individually:



            case $LINUX_CONF in
            (No | nO | NO | no) echo linux;;
            (*) echo not linux;;
            esac


            For a bash-specific way to do case-insensitive matching, you can do:



            shopt -s nocasematch
            [[ $LINUX_CONF = no ]] && echo linux


            Or:



            [[ $LINUX_CONF,, = no ]] && echo linux


            (where $VAR,, is the syntax to convert a string to lower case).



            You can also force a variable to be converted to lowercase upon assignment with:



            typeset -l LINUX_CONF


            That also comes from ksh and is also supported by bash and zsh.



            More variants with other shells:



            zsh



            set -o nocasematch
            [[ $LINUX_CONF =~ no ]] && echo linux


            (same as in bash).



            setopt extendedglob
            [[ $LINUX_CONF = (#i)no ]] && echo linux


            (less dangerous than making all matches case insensitive)



            [[ $(L)LINUX_CONF = no ]] && echo linux


            (convert to lowercase operator)



            set -o rematchpcre
            [[ $LINUX_CONF =~ (?i)no ]]


            (PCRE syntax)



            ksh93



            [[ $LINUX_CONF = ~(i)no ]]


            or



            [[ $LINUX_CONF = ~(i:no) ]]


            Note that all approaches above other than [nN][oO] to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.



            In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i and I letters and whether the dot is there or not on the uppercase or lowercase version.



            What that means is that [[ $VAR,, = oui ]] is not guaranteed to match on OUI in every locale (even when the bug in current versions of bash is fixed).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 2 '17 at 19:32

























            answered Oct 2 '17 at 16:12









            Stéphane Chazelas

            283k53522859




            283k53522859











            • ok fine but how we do the same in if syntax?
              – yael
              Oct 2 '17 at 16:13










            • @yael, why would you want to use if? If that's for the else part, see edit.
              – Stéphane Chazelas
              Oct 2 '17 at 16:15










            • There is also [[ "$LINUX_CONF" =~ [Nn][Oo] ]] (what with the OP specifically asking for a RE).
              – DopeGhoti
              Oct 2 '17 at 16:21







            • 1




              @DopeGhoti, or [[ $LINUX_CONF = [Nn][Oo] ]] but that has little advantage over the standard case syntax.
              – Stéphane Chazelas
              Oct 2 '17 at 16:22










            • In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih $var,,.
              – DopeGhoti
              Oct 2 '17 at 16:23
















            • ok fine but how we do the same in if syntax?
              – yael
              Oct 2 '17 at 16:13










            • @yael, why would you want to use if? If that's for the else part, see edit.
              – Stéphane Chazelas
              Oct 2 '17 at 16:15










            • There is also [[ "$LINUX_CONF" =~ [Nn][Oo] ]] (what with the OP specifically asking for a RE).
              – DopeGhoti
              Oct 2 '17 at 16:21







            • 1




              @DopeGhoti, or [[ $LINUX_CONF = [Nn][Oo] ]] but that has little advantage over the standard case syntax.
              – Stéphane Chazelas
              Oct 2 '17 at 16:22










            • In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih $var,,.
              – DopeGhoti
              Oct 2 '17 at 16:23















            ok fine but how we do the same in if syntax?
            – yael
            Oct 2 '17 at 16:13




            ok fine but how we do the same in if syntax?
            – yael
            Oct 2 '17 at 16:13












            @yael, why would you want to use if? If that's for the else part, see edit.
            – Stéphane Chazelas
            Oct 2 '17 at 16:15




            @yael, why would you want to use if? If that's for the else part, see edit.
            – Stéphane Chazelas
            Oct 2 '17 at 16:15












            There is also [[ "$LINUX_CONF" =~ [Nn][Oo] ]] (what with the OP specifically asking for a RE).
            – DopeGhoti
            Oct 2 '17 at 16:21





            There is also [[ "$LINUX_CONF" =~ [Nn][Oo] ]] (what with the OP specifically asking for a RE).
            – DopeGhoti
            Oct 2 '17 at 16:21





            1




            1




            @DopeGhoti, or [[ $LINUX_CONF = [Nn][Oo] ]] but that has little advantage over the standard case syntax.
            – Stéphane Chazelas
            Oct 2 '17 at 16:22




            @DopeGhoti, or [[ $LINUX_CONF = [Nn][Oo] ]] but that has little advantage over the standard case syntax.
            – Stéphane Chazelas
            Oct 2 '17 at 16:22












            In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih $var,,.
            – DopeGhoti
            Oct 2 '17 at 16:23




            In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih $var,,.
            – DopeGhoti
            Oct 2 '17 at 16:23












            up vote
            0
            down vote













            Keep your existing command but on the line before it run this:



            LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')


            Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.






            share|improve this answer
























              up vote
              0
              down vote













              Keep your existing command but on the line before it run this:



              LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')


              Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Keep your existing command but on the line before it run this:



                LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')


                Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.






                share|improve this answer












                Keep your existing command but on the line before it run this:



                LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')


                Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 3 '17 at 11:56









                EnterUserNameHere

                4318




                4318



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395685%2fbash-case-insensitive-matching-of-variable%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    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