Meaning of BASH operators “^”, “|” and “$” in condition ^(n|N)$

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











up vote
2
down vote

favorite












In my script, I am trying to make "Try again? [y/N]" condition...



if [[ "$response" =~ ^(n|no)$ ]]; then do something...,


but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.



Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.







share|improve this question


























    up vote
    2
    down vote

    favorite












    In my script, I am trying to make "Try again? [y/N]" condition...



    if [[ "$response" =~ ^(n|no)$ ]]; then do something...,


    but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.



    Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.







    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      In my script, I am trying to make "Try again? [y/N]" condition...



      if [[ "$response" =~ ^(n|no)$ ]]; then do something...,


      but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.



      Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.







      share|improve this question














      In my script, I am trying to make "Try again? [y/N]" condition...



      if [[ "$response" =~ ^(n|no)$ ]]; then do something...,


      but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.



      Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 at 23:44









      Jesse_b

      10.4k22658




      10.4k22658










      asked Mar 24 at 23:43









      Michal

      141




      141




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          8
          down vote













          These are regular expression special characters.



          ^ is an anchor for the start of string (So nothing can be before the match)



          $ is an anchor for the end of string (so nothing can be after the match)



          | is OR as you suspected



          The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.



          [[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.






          share|improve this answer


















          • 3




            Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes ‘nein’ to make the test true.
            – neal
            Mar 25 at 4:52










          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%2f433342%2fmeaning-of-bash-operators-and-in-condition-nn%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          8
          down vote













          These are regular expression special characters.



          ^ is an anchor for the start of string (So nothing can be before the match)



          $ is an anchor for the end of string (so nothing can be after the match)



          | is OR as you suspected



          The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.



          [[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.






          share|improve this answer


















          • 3




            Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes ‘nein’ to make the test true.
            – neal
            Mar 25 at 4:52














          up vote
          8
          down vote













          These are regular expression special characters.



          ^ is an anchor for the start of string (So nothing can be before the match)



          $ is an anchor for the end of string (so nothing can be after the match)



          | is OR as you suspected



          The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.



          [[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.






          share|improve this answer


















          • 3




            Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes ‘nein’ to make the test true.
            – neal
            Mar 25 at 4:52












          up vote
          8
          down vote










          up vote
          8
          down vote









          These are regular expression special characters.



          ^ is an anchor for the start of string (So nothing can be before the match)



          $ is an anchor for the end of string (so nothing can be after the match)



          | is OR as you suspected



          The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.



          [[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.






          share|improve this answer














          These are regular expression special characters.



          ^ is an anchor for the start of string (So nothing can be before the match)



          $ is an anchor for the end of string (so nothing can be after the match)



          | is OR as you suspected



          The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.



          [[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 11:03

























          answered Mar 24 at 23:46









          Jesse_b

          10.4k22658




          10.4k22658







          • 3




            Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes ‘nein’ to make the test true.
            – neal
            Mar 25 at 4:52












          • 3




            Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes ‘nein’ to make the test true.
            – neal
            Mar 25 at 4:52







          3




          3




          Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes ‘nein’ to make the test true.
          – neal
          Mar 25 at 4:52




          Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes ‘nein’ to make the test true.
          – neal
          Mar 25 at 4:52












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433342%2fmeaning-of-bash-operators-and-in-condition-nn%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)