What is the behavior of `getopts` when it meets `--`?

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











up vote
3
down vote

favorite












By convention -- signals that there is no more options after it.
It seems to me that when using getopts with case clause, -) pattern subclause doesn't match --. So what is the behavior of getopts when it meets --? Does it treat -- as an option, a nonoption argument, or neither? Thanks.







share|improve this question


























    up vote
    3
    down vote

    favorite












    By convention -- signals that there is no more options after it.
    It seems to me that when using getopts with case clause, -) pattern subclause doesn't match --. So what is the behavior of getopts when it meets --? Does it treat -- as an option, a nonoption argument, or neither? Thanks.







    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      By convention -- signals that there is no more options after it.
      It seems to me that when using getopts with case clause, -) pattern subclause doesn't match --. So what is the behavior of getopts when it meets --? Does it treat -- as an option, a nonoption argument, or neither? Thanks.







      share|improve this question














      By convention -- signals that there is no more options after it.
      It seems to me that when using getopts with case clause, -) pattern subclause doesn't match --. So what is the behavior of getopts when it meets --? Does it treat -- as an option, a nonoption argument, or neither? Thanks.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 25 at 19:11

























      asked Feb 25 at 19:02









      Tim

      22.7k64224401




      22.7k64224401




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          The behaviour is that it stops parsing the command line and leaves the rest of the arguments as is. The -- itself is removed (or rather $OPTIND will indicate that it was processed but $opt in the code below will never be -, and if you shift "$(( OPTIND - 1 ))" as one usually does, you'll never see it).



          Example:



          #!/bin/bash

          while getopts 'a:b:' opt; do
          case "$opt" in
          a) printf 'Got a: "%s"n' "$OPTARG" ;;
          b) printf 'Got b: "%s"n' "$OPTARG" ;;
          *) echo 'error' >&2
          exit 1
          esac
          done

          shift "$(( OPTIND - 1 ))"

          printf 'Other argument: "%s"n' "$@"


          Running it:



          $ bash script.sh -a hello -- -b world
          Got a: "hello"
          Other argument: "-b"
          Other argument: "world"


          As you can see, the -b world bit of the command line was not processed by getopts.



          It stops parsing the command line at -- or at the first non-option argument:



          $ bash script.sh something -a hello -- -b world
          Other argument: "something"
          Other argument: "-a"
          Other argument: "hello"
          Other argument: "--"
          Other argument: "-b"
          Other argument: "world"


          In this case, -- was not "removed" since getopts never got that far.






          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%2f426517%2fwhat-is-the-behavior-of-getopts-when-it-meets%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
            10
            down vote



            accepted










            The behaviour is that it stops parsing the command line and leaves the rest of the arguments as is. The -- itself is removed (or rather $OPTIND will indicate that it was processed but $opt in the code below will never be -, and if you shift "$(( OPTIND - 1 ))" as one usually does, you'll never see it).



            Example:



            #!/bin/bash

            while getopts 'a:b:' opt; do
            case "$opt" in
            a) printf 'Got a: "%s"n' "$OPTARG" ;;
            b) printf 'Got b: "%s"n' "$OPTARG" ;;
            *) echo 'error' >&2
            exit 1
            esac
            done

            shift "$(( OPTIND - 1 ))"

            printf 'Other argument: "%s"n' "$@"


            Running it:



            $ bash script.sh -a hello -- -b world
            Got a: "hello"
            Other argument: "-b"
            Other argument: "world"


            As you can see, the -b world bit of the command line was not processed by getopts.



            It stops parsing the command line at -- or at the first non-option argument:



            $ bash script.sh something -a hello -- -b world
            Other argument: "something"
            Other argument: "-a"
            Other argument: "hello"
            Other argument: "--"
            Other argument: "-b"
            Other argument: "world"


            In this case, -- was not "removed" since getopts never got that far.






            share|improve this answer


























              up vote
              10
              down vote



              accepted










              The behaviour is that it stops parsing the command line and leaves the rest of the arguments as is. The -- itself is removed (or rather $OPTIND will indicate that it was processed but $opt in the code below will never be -, and if you shift "$(( OPTIND - 1 ))" as one usually does, you'll never see it).



              Example:



              #!/bin/bash

              while getopts 'a:b:' opt; do
              case "$opt" in
              a) printf 'Got a: "%s"n' "$OPTARG" ;;
              b) printf 'Got b: "%s"n' "$OPTARG" ;;
              *) echo 'error' >&2
              exit 1
              esac
              done

              shift "$(( OPTIND - 1 ))"

              printf 'Other argument: "%s"n' "$@"


              Running it:



              $ bash script.sh -a hello -- -b world
              Got a: "hello"
              Other argument: "-b"
              Other argument: "world"


              As you can see, the -b world bit of the command line was not processed by getopts.



              It stops parsing the command line at -- or at the first non-option argument:



              $ bash script.sh something -a hello -- -b world
              Other argument: "something"
              Other argument: "-a"
              Other argument: "hello"
              Other argument: "--"
              Other argument: "-b"
              Other argument: "world"


              In this case, -- was not "removed" since getopts never got that far.






              share|improve this answer
























                up vote
                10
                down vote



                accepted







                up vote
                10
                down vote



                accepted






                The behaviour is that it stops parsing the command line and leaves the rest of the arguments as is. The -- itself is removed (or rather $OPTIND will indicate that it was processed but $opt in the code below will never be -, and if you shift "$(( OPTIND - 1 ))" as one usually does, you'll never see it).



                Example:



                #!/bin/bash

                while getopts 'a:b:' opt; do
                case "$opt" in
                a) printf 'Got a: "%s"n' "$OPTARG" ;;
                b) printf 'Got b: "%s"n' "$OPTARG" ;;
                *) echo 'error' >&2
                exit 1
                esac
                done

                shift "$(( OPTIND - 1 ))"

                printf 'Other argument: "%s"n' "$@"


                Running it:



                $ bash script.sh -a hello -- -b world
                Got a: "hello"
                Other argument: "-b"
                Other argument: "world"


                As you can see, the -b world bit of the command line was not processed by getopts.



                It stops parsing the command line at -- or at the first non-option argument:



                $ bash script.sh something -a hello -- -b world
                Other argument: "something"
                Other argument: "-a"
                Other argument: "hello"
                Other argument: "--"
                Other argument: "-b"
                Other argument: "world"


                In this case, -- was not "removed" since getopts never got that far.






                share|improve this answer














                The behaviour is that it stops parsing the command line and leaves the rest of the arguments as is. The -- itself is removed (or rather $OPTIND will indicate that it was processed but $opt in the code below will never be -, and if you shift "$(( OPTIND - 1 ))" as one usually does, you'll never see it).



                Example:



                #!/bin/bash

                while getopts 'a:b:' opt; do
                case "$opt" in
                a) printf 'Got a: "%s"n' "$OPTARG" ;;
                b) printf 'Got b: "%s"n' "$OPTARG" ;;
                *) echo 'error' >&2
                exit 1
                esac
                done

                shift "$(( OPTIND - 1 ))"

                printf 'Other argument: "%s"n' "$@"


                Running it:



                $ bash script.sh -a hello -- -b world
                Got a: "hello"
                Other argument: "-b"
                Other argument: "world"


                As you can see, the -b world bit of the command line was not processed by getopts.



                It stops parsing the command line at -- or at the first non-option argument:



                $ bash script.sh something -a hello -- -b world
                Other argument: "something"
                Other argument: "-a"
                Other argument: "hello"
                Other argument: "--"
                Other argument: "-b"
                Other argument: "world"


                In this case, -- was not "removed" since getopts never got that far.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 25 at 20:52









                Stéphane Chazelas

                280k53516847




                280k53516847










                answered Feb 25 at 19:08









                Kusalananda

                103k13202318




                103k13202318






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426517%2fwhat-is-the-behavior-of-getopts-when-it-meets%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