Using a variable as a case condition in zsh

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











up vote
5
down vote

favorite












My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:



input="foo"
pattern="(foo|bar)"

case $input in
$pattern)
echo "you sent foo or bar"
;;
*)
echo "foo or bar was not sent"
;;
esac


I would like to use the strings foo or bar and have the above code execute the pattern case condition.







share|improve this question

























    up vote
    5
    down vote

    favorite












    My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:



    input="foo"
    pattern="(foo|bar)"

    case $input in
    $pattern)
    echo "you sent foo or bar"
    ;;
    *)
    echo "foo or bar was not sent"
    ;;
    esac


    I would like to use the strings foo or bar and have the above code execute the pattern case condition.







    share|improve this question























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:



      input="foo"
      pattern="(foo|bar)"

      case $input in
      $pattern)
      echo "you sent foo or bar"
      ;;
      *)
      echo "foo or bar was not sent"
      ;;
      esac


      I would like to use the strings foo or bar and have the above code execute the pattern case condition.







      share|improve this question













      My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:



      input="foo"
      pattern="(foo|bar)"

      case $input in
      $pattern)
      echo "you sent foo or bar"
      ;;
      *)
      echo "foo or bar was not sent"
      ;;
      esac


      I would like to use the strings foo or bar and have the above code execute the pattern case condition.









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 30 at 21:36









      Gilles

      503k1179951521




      503k1179951521









      asked May 28 at 0:24









      Smashgen

      984




      984




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          With this code saved to the file first,



          pattern=fo*
          input=foo
          case $input in
          $pattern)
          print T
          ;;
          fo*)
          print NIL
          ;;
          esac


          under -x we may observe that the variable appears as a quoted value while the raw expression does not:



          % zsh -x first
          +first:1> pattern='fo*'
          +first:2> input=foo
          +first:3> case foo (fo*)
          +first:3> case foo (fo*)
          +first:8> print NIL
          NIL


          That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1) one might be aware of the glob substitution flag



           $~spec
          Turn on the GLOB_SUBST option for the evaluation of spec; if the
          `~' is doubled, turn it off. When this option is set, the
          string resulting from the expansion will be interpreted as a
          pattern anywhere that is possible,


          so if we modify $pattern to use that



          pattern=fo*
          input=foo
          case $input in
          $~pattern) # !
          print T
          ;;
          fo*)
          print NIL
          ;;
          esac


          we see instead



          % zsh -x second
          +second:1> pattern='fo*'
          +second:2> input=foo
          +second:3> case foo (fo*)
          +second:5> print T
          T


          for your case the pattern must be quoted:



          pattern='(foo|bar)'
          input=foo
          case $input in
          $~pattern)
          print T
          ;;
          *)
          print NIL
          ;;
          esac





          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%2f446374%2fusing-a-variable-as-a-case-condition-in-zsh%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
            6
            down vote



            accepted










            With this code saved to the file first,



            pattern=fo*
            input=foo
            case $input in
            $pattern)
            print T
            ;;
            fo*)
            print NIL
            ;;
            esac


            under -x we may observe that the variable appears as a quoted value while the raw expression does not:



            % zsh -x first
            +first:1> pattern='fo*'
            +first:2> input=foo
            +first:3> case foo (fo*)
            +first:3> case foo (fo*)
            +first:8> print NIL
            NIL


            That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1) one might be aware of the glob substitution flag



             $~spec
            Turn on the GLOB_SUBST option for the evaluation of spec; if the
            `~' is doubled, turn it off. When this option is set, the
            string resulting from the expansion will be interpreted as a
            pattern anywhere that is possible,


            so if we modify $pattern to use that



            pattern=fo*
            input=foo
            case $input in
            $~pattern) # !
            print T
            ;;
            fo*)
            print NIL
            ;;
            esac


            we see instead



            % zsh -x second
            +second:1> pattern='fo*'
            +second:2> input=foo
            +second:3> case foo (fo*)
            +second:5> print T
            T


            for your case the pattern must be quoted:



            pattern='(foo|bar)'
            input=foo
            case $input in
            $~pattern)
            print T
            ;;
            *)
            print NIL
            ;;
            esac





            share|improve this answer

























              up vote
              6
              down vote



              accepted










              With this code saved to the file first,



              pattern=fo*
              input=foo
              case $input in
              $pattern)
              print T
              ;;
              fo*)
              print NIL
              ;;
              esac


              under -x we may observe that the variable appears as a quoted value while the raw expression does not:



              % zsh -x first
              +first:1> pattern='fo*'
              +first:2> input=foo
              +first:3> case foo (fo*)
              +first:3> case foo (fo*)
              +first:8> print NIL
              NIL


              That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1) one might be aware of the glob substitution flag



               $~spec
              Turn on the GLOB_SUBST option for the evaluation of spec; if the
              `~' is doubled, turn it off. When this option is set, the
              string resulting from the expansion will be interpreted as a
              pattern anywhere that is possible,


              so if we modify $pattern to use that



              pattern=fo*
              input=foo
              case $input in
              $~pattern) # !
              print T
              ;;
              fo*)
              print NIL
              ;;
              esac


              we see instead



              % zsh -x second
              +second:1> pattern='fo*'
              +second:2> input=foo
              +second:3> case foo (fo*)
              +second:5> print T
              T


              for your case the pattern must be quoted:



              pattern='(foo|bar)'
              input=foo
              case $input in
              $~pattern)
              print T
              ;;
              *)
              print NIL
              ;;
              esac





              share|improve this answer























                up vote
                6
                down vote



                accepted







                up vote
                6
                down vote



                accepted






                With this code saved to the file first,



                pattern=fo*
                input=foo
                case $input in
                $pattern)
                print T
                ;;
                fo*)
                print NIL
                ;;
                esac


                under -x we may observe that the variable appears as a quoted value while the raw expression does not:



                % zsh -x first
                +first:1> pattern='fo*'
                +first:2> input=foo
                +first:3> case foo (fo*)
                +first:3> case foo (fo*)
                +first:8> print NIL
                NIL


                That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1) one might be aware of the glob substitution flag



                 $~spec
                Turn on the GLOB_SUBST option for the evaluation of spec; if the
                `~' is doubled, turn it off. When this option is set, the
                string resulting from the expansion will be interpreted as a
                pattern anywhere that is possible,


                so if we modify $pattern to use that



                pattern=fo*
                input=foo
                case $input in
                $~pattern) # !
                print T
                ;;
                fo*)
                print NIL
                ;;
                esac


                we see instead



                % zsh -x second
                +second:1> pattern='fo*'
                +second:2> input=foo
                +second:3> case foo (fo*)
                +second:5> print T
                T


                for your case the pattern must be quoted:



                pattern='(foo|bar)'
                input=foo
                case $input in
                $~pattern)
                print T
                ;;
                *)
                print NIL
                ;;
                esac





                share|improve this answer













                With this code saved to the file first,



                pattern=fo*
                input=foo
                case $input in
                $pattern)
                print T
                ;;
                fo*)
                print NIL
                ;;
                esac


                under -x we may observe that the variable appears as a quoted value while the raw expression does not:



                % zsh -x first
                +first:1> pattern='fo*'
                +first:2> input=foo
                +first:3> case foo (fo*)
                +first:3> case foo (fo*)
                +first:8> print NIL
                NIL


                That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1) one might be aware of the glob substitution flag



                 $~spec
                Turn on the GLOB_SUBST option for the evaluation of spec; if the
                `~' is doubled, turn it off. When this option is set, the
                string resulting from the expansion will be interpreted as a
                pattern anywhere that is possible,


                so if we modify $pattern to use that



                pattern=fo*
                input=foo
                case $input in
                $~pattern) # !
                print T
                ;;
                fo*)
                print NIL
                ;;
                esac


                we see instead



                % zsh -x second
                +second:1> pattern='fo*'
                +second:2> input=foo
                +second:3> case foo (fo*)
                +second:5> print T
                T


                for your case the pattern must be quoted:



                pattern='(foo|bar)'
                input=foo
                case $input in
                $~pattern)
                print T
                ;;
                *)
                print NIL
                ;;
                esac






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 28 at 2:41









                thrig

                21.9k12751




                21.9k12751






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446374%2fusing-a-variable-as-a-case-condition-in-zsh%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?