How to check if a a variable contains both alphabets and numbers

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












0















I am trying to make a check in a shell script on linux which exits if STR contains "only alphabets" or contains "only numbers". it should pass if STR contains both numbers and alphabets. And not contain special characters.



I am trying it with something like this but it works only partially.



#!/bin/sh

STR=$1

if [[ ! $STR =~ ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ ]]; then
echo "The input must contain both digits and alphabets like abc123"
exit 1
fi

echo "Success"
exit 0


The check works partially, that is if STR contains only numbers. But does not work if STR contains all alphabets. I want exit 1 to not execute only if STR contains both alphabets and numbers like abc123 or ABC123.



The check fails if STR contains abc.



How can I make the check work when STR contains abc only as well as when STR contains 123 only?










share|improve this question
























  • pro tip, don't actually name your script "test", unless you're careful about how you invoke it :)

    – Jeff Schaller
    Mar 6 at 12:06











  • note that the "alnum" includes "alpha" and "numerics" so if you really want to see a number, "alnum" isn't the best choice

    – Jeff Schaller
    Mar 6 at 12:07















0















I am trying to make a check in a shell script on linux which exits if STR contains "only alphabets" or contains "only numbers". it should pass if STR contains both numbers and alphabets. And not contain special characters.



I am trying it with something like this but it works only partially.



#!/bin/sh

STR=$1

if [[ ! $STR =~ ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ ]]; then
echo "The input must contain both digits and alphabets like abc123"
exit 1
fi

echo "Success"
exit 0


The check works partially, that is if STR contains only numbers. But does not work if STR contains all alphabets. I want exit 1 to not execute only if STR contains both alphabets and numbers like abc123 or ABC123.



The check fails if STR contains abc.



How can I make the check work when STR contains abc only as well as when STR contains 123 only?










share|improve this question
























  • pro tip, don't actually name your script "test", unless you're careful about how you invoke it :)

    – Jeff Schaller
    Mar 6 at 12:06











  • note that the "alnum" includes "alpha" and "numerics" so if you really want to see a number, "alnum" isn't the best choice

    – Jeff Schaller
    Mar 6 at 12:07













0












0








0








I am trying to make a check in a shell script on linux which exits if STR contains "only alphabets" or contains "only numbers". it should pass if STR contains both numbers and alphabets. And not contain special characters.



I am trying it with something like this but it works only partially.



#!/bin/sh

STR=$1

if [[ ! $STR =~ ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ ]]; then
echo "The input must contain both digits and alphabets like abc123"
exit 1
fi

echo "Success"
exit 0


The check works partially, that is if STR contains only numbers. But does not work if STR contains all alphabets. I want exit 1 to not execute only if STR contains both alphabets and numbers like abc123 or ABC123.



The check fails if STR contains abc.



How can I make the check work when STR contains abc only as well as when STR contains 123 only?










share|improve this question
















I am trying to make a check in a shell script on linux which exits if STR contains "only alphabets" or contains "only numbers". it should pass if STR contains both numbers and alphabets. And not contain special characters.



I am trying it with something like this but it works only partially.



#!/bin/sh

STR=$1

if [[ ! $STR =~ ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ ]]; then
echo "The input must contain both digits and alphabets like abc123"
exit 1
fi

echo "Success"
exit 0


The check works partially, that is if STR contains only numbers. But does not work if STR contains all alphabets. I want exit 1 to not execute only if STR contains both alphabets and numbers like abc123 or ABC123.



The check fails if STR contains abc.



How can I make the check work when STR contains abc only as well as when STR contains 123 only?







linux bash shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 12:23







Game_Of_Threads

















asked Mar 6 at 11:54









Game_Of_ThreadsGame_Of_Threads

1106




1106












  • pro tip, don't actually name your script "test", unless you're careful about how you invoke it :)

    – Jeff Schaller
    Mar 6 at 12:06











  • note that the "alnum" includes "alpha" and "numerics" so if you really want to see a number, "alnum" isn't the best choice

    – Jeff Schaller
    Mar 6 at 12:07

















  • pro tip, don't actually name your script "test", unless you're careful about how you invoke it :)

    – Jeff Schaller
    Mar 6 at 12:06











  • note that the "alnum" includes "alpha" and "numerics" so if you really want to see a number, "alnum" isn't the best choice

    – Jeff Schaller
    Mar 6 at 12:07
















pro tip, don't actually name your script "test", unless you're careful about how you invoke it :)

– Jeff Schaller
Mar 6 at 12:06





pro tip, don't actually name your script "test", unless you're careful about how you invoke it :)

– Jeff Schaller
Mar 6 at 12:06













note that the "alnum" includes "alpha" and "numerics" so if you really want to see a number, "alnum" isn't the best choice

– Jeff Schaller
Mar 6 at 12:07





note that the "alnum" includes "alpha" and "numerics" so if you really want to see a number, "alnum" isn't the best choice

– Jeff Schaller
Mar 6 at 12:07










3 Answers
3






active

oldest

votes


















2














The regex ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ doesn't check for numbers separately, since [:alnum:] matches also letters. [:alpha:] should be a subset of [:alnum:], so the pattern matches any string that is all alphanumerics, with at least one letter.



(Assuming the usual greedy matching, if the input is abcd, then the abc will be matched by [[:alnum:]]*, the final d will be matched by [[:alpha:]] and the last [[:alnum:]]* will not (need to) match anything.)



If you want to check that the string contains at least one letter and one digit, it's easier to separate that to two tests, i.e. test for [[:alpha:]] and [[:digit:]]:



if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] ]]; then
echo "contains a letter and a digit"


If you also want to reject strings that contain anything else (like punctuation), add a check for that:



if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] && ! $str =~ [^[:alnum:]] ]]; then
echo "contains a letter and a digit, but no non-alphanumerics"


To find the failing cases, invert the whole test with a ! at the start (if ! [[ ...).



(To do that with one regex, you could use something like this:
^([[:alnum:]]*[[:alpha:]][[:alnum:]]*[[:digit:]][[:alnum:]]*|[[:alnum:]]*[[:digit:]][[:alnum:]]*[[:alpha:]][[:alnum:]]*)$
but that's a bit horrible)






share|improve this answer
































    3














    You don't need to use complicated regular expressions here. You have two conditions, so use two tests:



    if [[ $str == *[[:digit:]]* ]] &&
    [[ $str == *[[:alpha:]]* ]]
    then
    printf '"%s" contains both letters and digitsn' "$str"
    else
    printf '"%s" lacks either letters or digitsn' "$str"
    fi


    You also say something about "special characters" but you don't specify what these are. Assuming you mean characters matched by [[:punct:]], and that you don't want these in the string, you could use



    if [[ $str == *[[:digit:]]* ]] &&
    [[ $str == *[[:alpha:]]* ]] &&
    [[ $str != *[[:punct:]]* ]]
    then
    printf '"%s" contains both letters and digits, and no specialsn' "$str"
    else
    printf '"%s" lacks either letters or digits, or contains specialsn' "$str"
    fi


    [[:punct:]] would match any one of the characters in the string



    !"#$%&'()*+,-./:;<=>?@[]^_`~





    share|improve this answer
































      0














      Try to make it simpler. Separate the question in two:



      ...
      if [[ $STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+) ]]; then
      ...





      share|improve this answer























      • You are missing the not statement that is !. The not clause is important for me here to understand how this works

        – Game_Of_Threads
        Mar 6 at 12:37











      • Figured out the not . if [[ !($STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+)) ]]; then it should be then

        – Game_Of_Threads
        Mar 6 at 12:40











      • As far as I understand your question, you do NOT need the "not". The "if" I posted means: "do I have any letter here (normal or caps)? ... and also do I have any number here?" which is exaclty what you wrote in the description: "I want exit 1 ... only if STR contains both alphabets and numbers"

        – Juan
        Mar 6 at 12:42











      • Actually, if you put that "not", you will destroy the logic.

        – Juan
        Mar 6 at 13:14











      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      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%2f504686%2fhow-to-check-if-a-a-variable-contains-both-alphabets-and-numbers%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      The regex ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ doesn't check for numbers separately, since [:alnum:] matches also letters. [:alpha:] should be a subset of [:alnum:], so the pattern matches any string that is all alphanumerics, with at least one letter.



      (Assuming the usual greedy matching, if the input is abcd, then the abc will be matched by [[:alnum:]]*, the final d will be matched by [[:alpha:]] and the last [[:alnum:]]* will not (need to) match anything.)



      If you want to check that the string contains at least one letter and one digit, it's easier to separate that to two tests, i.e. test for [[:alpha:]] and [[:digit:]]:



      if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] ]]; then
      echo "contains a letter and a digit"


      If you also want to reject strings that contain anything else (like punctuation), add a check for that:



      if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] && ! $str =~ [^[:alnum:]] ]]; then
      echo "contains a letter and a digit, but no non-alphanumerics"


      To find the failing cases, invert the whole test with a ! at the start (if ! [[ ...).



      (To do that with one regex, you could use something like this:
      ^([[:alnum:]]*[[:alpha:]][[:alnum:]]*[[:digit:]][[:alnum:]]*|[[:alnum:]]*[[:digit:]][[:alnum:]]*[[:alpha:]][[:alnum:]]*)$
      but that's a bit horrible)






      share|improve this answer





























        2














        The regex ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ doesn't check for numbers separately, since [:alnum:] matches also letters. [:alpha:] should be a subset of [:alnum:], so the pattern matches any string that is all alphanumerics, with at least one letter.



        (Assuming the usual greedy matching, if the input is abcd, then the abc will be matched by [[:alnum:]]*, the final d will be matched by [[:alpha:]] and the last [[:alnum:]]* will not (need to) match anything.)



        If you want to check that the string contains at least one letter and one digit, it's easier to separate that to two tests, i.e. test for [[:alpha:]] and [[:digit:]]:



        if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] ]]; then
        echo "contains a letter and a digit"


        If you also want to reject strings that contain anything else (like punctuation), add a check for that:



        if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] && ! $str =~ [^[:alnum:]] ]]; then
        echo "contains a letter and a digit, but no non-alphanumerics"


        To find the failing cases, invert the whole test with a ! at the start (if ! [[ ...).



        (To do that with one regex, you could use something like this:
        ^([[:alnum:]]*[[:alpha:]][[:alnum:]]*[[:digit:]][[:alnum:]]*|[[:alnum:]]*[[:digit:]][[:alnum:]]*[[:alpha:]][[:alnum:]]*)$
        but that's a bit horrible)






        share|improve this answer



























          2












          2








          2







          The regex ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ doesn't check for numbers separately, since [:alnum:] matches also letters. [:alpha:] should be a subset of [:alnum:], so the pattern matches any string that is all alphanumerics, with at least one letter.



          (Assuming the usual greedy matching, if the input is abcd, then the abc will be matched by [[:alnum:]]*, the final d will be matched by [[:alpha:]] and the last [[:alnum:]]* will not (need to) match anything.)



          If you want to check that the string contains at least one letter and one digit, it's easier to separate that to two tests, i.e. test for [[:alpha:]] and [[:digit:]]:



          if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] ]]; then
          echo "contains a letter and a digit"


          If you also want to reject strings that contain anything else (like punctuation), add a check for that:



          if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] && ! $str =~ [^[:alnum:]] ]]; then
          echo "contains a letter and a digit, but no non-alphanumerics"


          To find the failing cases, invert the whole test with a ! at the start (if ! [[ ...).



          (To do that with one regex, you could use something like this:
          ^([[:alnum:]]*[[:alpha:]][[:alnum:]]*[[:digit:]][[:alnum:]]*|[[:alnum:]]*[[:digit:]][[:alnum:]]*[[:alpha:]][[:alnum:]]*)$
          but that's a bit horrible)






          share|improve this answer















          The regex ^[[:alnum:]]*[[:alpha:]][[:alnum:]]*$ doesn't check for numbers separately, since [:alnum:] matches also letters. [:alpha:] should be a subset of [:alnum:], so the pattern matches any string that is all alphanumerics, with at least one letter.



          (Assuming the usual greedy matching, if the input is abcd, then the abc will be matched by [[:alnum:]]*, the final d will be matched by [[:alpha:]] and the last [[:alnum:]]* will not (need to) match anything.)



          If you want to check that the string contains at least one letter and one digit, it's easier to separate that to two tests, i.e. test for [[:alpha:]] and [[:digit:]]:



          if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] ]]; then
          echo "contains a letter and a digit"


          If you also want to reject strings that contain anything else (like punctuation), add a check for that:



          if [[ $str =~ [[:alpha:]] && $str =~ [[:digit:]] && ! $str =~ [^[:alnum:]] ]]; then
          echo "contains a letter and a digit, but no non-alphanumerics"


          To find the failing cases, invert the whole test with a ! at the start (if ! [[ ...).



          (To do that with one regex, you could use something like this:
          ^([[:alnum:]]*[[:alpha:]][[:alnum:]]*[[:digit:]][[:alnum:]]*|[[:alnum:]]*[[:digit:]][[:alnum:]]*[[:alpha:]][[:alnum:]]*)$
          but that's a bit horrible)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 6 at 13:07

























          answered Mar 6 at 12:55









          ilkkachuilkkachu

          63k10103180




          63k10103180























              3














              You don't need to use complicated regular expressions here. You have two conditions, so use two tests:



              if [[ $str == *[[:digit:]]* ]] &&
              [[ $str == *[[:alpha:]]* ]]
              then
              printf '"%s" contains both letters and digitsn' "$str"
              else
              printf '"%s" lacks either letters or digitsn' "$str"
              fi


              You also say something about "special characters" but you don't specify what these are. Assuming you mean characters matched by [[:punct:]], and that you don't want these in the string, you could use



              if [[ $str == *[[:digit:]]* ]] &&
              [[ $str == *[[:alpha:]]* ]] &&
              [[ $str != *[[:punct:]]* ]]
              then
              printf '"%s" contains both letters and digits, and no specialsn' "$str"
              else
              printf '"%s" lacks either letters or digits, or contains specialsn' "$str"
              fi


              [[:punct:]] would match any one of the characters in the string



              !"#$%&'()*+,-./:;<=>?@[]^_`~





              share|improve this answer





























                3














                You don't need to use complicated regular expressions here. You have two conditions, so use two tests:



                if [[ $str == *[[:digit:]]* ]] &&
                [[ $str == *[[:alpha:]]* ]]
                then
                printf '"%s" contains both letters and digitsn' "$str"
                else
                printf '"%s" lacks either letters or digitsn' "$str"
                fi


                You also say something about "special characters" but you don't specify what these are. Assuming you mean characters matched by [[:punct:]], and that you don't want these in the string, you could use



                if [[ $str == *[[:digit:]]* ]] &&
                [[ $str == *[[:alpha:]]* ]] &&
                [[ $str != *[[:punct:]]* ]]
                then
                printf '"%s" contains both letters and digits, and no specialsn' "$str"
                else
                printf '"%s" lacks either letters or digits, or contains specialsn' "$str"
                fi


                [[:punct:]] would match any one of the characters in the string



                !"#$%&'()*+,-./:;<=>?@[]^_`~





                share|improve this answer



























                  3












                  3








                  3







                  You don't need to use complicated regular expressions here. You have two conditions, so use two tests:



                  if [[ $str == *[[:digit:]]* ]] &&
                  [[ $str == *[[:alpha:]]* ]]
                  then
                  printf '"%s" contains both letters and digitsn' "$str"
                  else
                  printf '"%s" lacks either letters or digitsn' "$str"
                  fi


                  You also say something about "special characters" but you don't specify what these are. Assuming you mean characters matched by [[:punct:]], and that you don't want these in the string, you could use



                  if [[ $str == *[[:digit:]]* ]] &&
                  [[ $str == *[[:alpha:]]* ]] &&
                  [[ $str != *[[:punct:]]* ]]
                  then
                  printf '"%s" contains both letters and digits, and no specialsn' "$str"
                  else
                  printf '"%s" lacks either letters or digits, or contains specialsn' "$str"
                  fi


                  [[:punct:]] would match any one of the characters in the string



                  !"#$%&'()*+,-./:;<=>?@[]^_`~





                  share|improve this answer















                  You don't need to use complicated regular expressions here. You have two conditions, so use two tests:



                  if [[ $str == *[[:digit:]]* ]] &&
                  [[ $str == *[[:alpha:]]* ]]
                  then
                  printf '"%s" contains both letters and digitsn' "$str"
                  else
                  printf '"%s" lacks either letters or digitsn' "$str"
                  fi


                  You also say something about "special characters" but you don't specify what these are. Assuming you mean characters matched by [[:punct:]], and that you don't want these in the string, you could use



                  if [[ $str == *[[:digit:]]* ]] &&
                  [[ $str == *[[:alpha:]]* ]] &&
                  [[ $str != *[[:punct:]]* ]]
                  then
                  printf '"%s" contains both letters and digits, and no specialsn' "$str"
                  else
                  printf '"%s" lacks either letters or digits, or contains specialsn' "$str"
                  fi


                  [[:punct:]] would match any one of the characters in the string



                  !"#$%&'()*+,-./:;<=>?@[]^_`~






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 6 at 12:55

























                  answered Mar 6 at 12:50









                  KusalanandaKusalananda

                  139k17259431




                  139k17259431





















                      0














                      Try to make it simpler. Separate the question in two:



                      ...
                      if [[ $STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+) ]]; then
                      ...





                      share|improve this answer























                      • You are missing the not statement that is !. The not clause is important for me here to understand how this works

                        – Game_Of_Threads
                        Mar 6 at 12:37











                      • Figured out the not . if [[ !($STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+)) ]]; then it should be then

                        – Game_Of_Threads
                        Mar 6 at 12:40











                      • As far as I understand your question, you do NOT need the "not". The "if" I posted means: "do I have any letter here (normal or caps)? ... and also do I have any number here?" which is exaclty what you wrote in the description: "I want exit 1 ... only if STR contains both alphabets and numbers"

                        – Juan
                        Mar 6 at 12:42











                      • Actually, if you put that "not", you will destroy the logic.

                        – Juan
                        Mar 6 at 13:14















                      0














                      Try to make it simpler. Separate the question in two:



                      ...
                      if [[ $STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+) ]]; then
                      ...





                      share|improve this answer























                      • You are missing the not statement that is !. The not clause is important for me here to understand how this works

                        – Game_Of_Threads
                        Mar 6 at 12:37











                      • Figured out the not . if [[ !($STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+)) ]]; then it should be then

                        – Game_Of_Threads
                        Mar 6 at 12:40











                      • As far as I understand your question, you do NOT need the "not". The "if" I posted means: "do I have any letter here (normal or caps)? ... and also do I have any number here?" which is exaclty what you wrote in the description: "I want exit 1 ... only if STR contains both alphabets and numbers"

                        – Juan
                        Mar 6 at 12:42











                      • Actually, if you put that "not", you will destroy the logic.

                        – Juan
                        Mar 6 at 13:14













                      0












                      0








                      0







                      Try to make it simpler. Separate the question in two:



                      ...
                      if [[ $STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+) ]]; then
                      ...





                      share|improve this answer













                      Try to make it simpler. Separate the question in two:



                      ...
                      if [[ $STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+) ]]; then
                      ...






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 6 at 12:32









                      JuanJuan

                      201210




                      201210












                      • You are missing the not statement that is !. The not clause is important for me here to understand how this works

                        – Game_Of_Threads
                        Mar 6 at 12:37











                      • Figured out the not . if [[ !($STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+)) ]]; then it should be then

                        – Game_Of_Threads
                        Mar 6 at 12:40











                      • As far as I understand your question, you do NOT need the "not". The "if" I posted means: "do I have any letter here (normal or caps)? ... and also do I have any number here?" which is exaclty what you wrote in the description: "I want exit 1 ... only if STR contains both alphabets and numbers"

                        – Juan
                        Mar 6 at 12:42











                      • Actually, if you put that "not", you will destroy the logic.

                        – Juan
                        Mar 6 at 13:14

















                      • You are missing the not statement that is !. The not clause is important for me here to understand how this works

                        – Game_Of_Threads
                        Mar 6 at 12:37











                      • Figured out the not . if [[ !($STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+)) ]]; then it should be then

                        – Game_Of_Threads
                        Mar 6 at 12:40











                      • As far as I understand your question, you do NOT need the "not". The "if" I posted means: "do I have any letter here (normal or caps)? ... and also do I have any number here?" which is exaclty what you wrote in the description: "I want exit 1 ... only if STR contains both alphabets and numbers"

                        – Juan
                        Mar 6 at 12:42











                      • Actually, if you put that "not", you will destroy the logic.

                        – Juan
                        Mar 6 at 13:14
















                      You are missing the not statement that is !. The not clause is important for me here to understand how this works

                      – Game_Of_Threads
                      Mar 6 at 12:37





                      You are missing the not statement that is !. The not clause is important for me here to understand how this works

                      – Game_Of_Threads
                      Mar 6 at 12:37













                      Figured out the not . if [[ !($STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+)) ]]; then it should be then

                      – Game_Of_Threads
                      Mar 6 at 12:40





                      Figured out the not . if [[ !($STR =~ ([a-zA-Z]+) && $STR =~ ([0-9]+)) ]]; then it should be then

                      – Game_Of_Threads
                      Mar 6 at 12:40













                      As far as I understand your question, you do NOT need the "not". The "if" I posted means: "do I have any letter here (normal or caps)? ... and also do I have any number here?" which is exaclty what you wrote in the description: "I want exit 1 ... only if STR contains both alphabets and numbers"

                      – Juan
                      Mar 6 at 12:42





                      As far as I understand your question, you do NOT need the "not". The "if" I posted means: "do I have any letter here (normal or caps)? ... and also do I have any number here?" which is exaclty what you wrote in the description: "I want exit 1 ... only if STR contains both alphabets and numbers"

                      – Juan
                      Mar 6 at 12:42













                      Actually, if you put that "not", you will destroy the logic.

                      – Juan
                      Mar 6 at 13:14





                      Actually, if you put that "not", you will destroy the logic.

                      – Juan
                      Mar 6 at 13:14

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504686%2fhow-to-check-if-a-a-variable-contains-both-alphabets-and-numbers%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)