Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for answer to How do I parse command line arguments in Bash? by Robert Siemer

Current License: CC BY-SA 4.0

Post Revisions

73 events
when toggle format what by license comment
May 30, 2025 at 12:27 history edited Robert Siemer CC BY-SA 4.0
clarify what request/feature rules out which solution
Jan 28, 2025 at 20:11 review Suggested edits
Jan 29, 2025 at 14:02
S Jan 27, 2025 at 15:20 history suggested Get rid of LLM slop CC BY-SA 4.0
Correct comment about getopt printing errors to stdout
Jan 26, 2025 at 17:05 review Suggested edits
S Jan 27, 2025 at 15:20
May 3, 2024 at 10:36 comment added Software Engineer @RobertSiemer - a 9-year old answer and you're still answering comments about it like a boss and keeping the answer up to date. Just thought that deserves being called out.
May 1, 2024 at 22:24 history edited Robert Siemer CC BY-SA 4.0
replaced PIPESTATUS and ! with equivalent but cleaner || and &&
Nov 23, 2023 at 21:51 history rollback Robert Siemer
Rollback to Revision 47
Nov 14, 2023 at 10:36 history edited VasiliNovikov CC BY-SA 4.0
Make the answer to be an answer, not a criticism of other answers. Make the text clearer.
May 4, 2023 at 11:42 history edited Robert Siemer CC BY-SA 4.0
rectifying defacing edit: OS X stays in the footnote exclusively, links have the same style incl. in markdown, do not spread OS X install instructions in multiple places, the preinstalled advantage does not apply to OS X either
May 3, 2023 at 17:02 comment added Johnny Utahh bobpaul@github's update to @Kaushal Modi's above getopt-example script: gist.github.com/bobpaul/ecd74cdf7681516703f20726431eaceb
May 2, 2023 at 22:53 history edited Johnny Utahh CC BY-SA 4.0
adding https://formulae.brew.sh/formula/util-linux to appropriate footnote area
May 2, 2023 at 18:29 history edited Johnny Utahh CC BY-SA 4.0
sharing which macOS Homebrew package has a working (proper?) getopt
May 2, 2023 at 18:25 comment added Johnny Utahh @taiyodayo - brew install util-linux and then adding /opt/homebrew/opt/util-linux/bin--or better yet, $(brew --prefix util-linux)/bin--to PATH yields a properly-working (getopt --test yields 4 return value) getopt for me on macOS v12.6.3.
Nov 16, 2022 at 8:50 comment added Robert Siemer @Boffin yes, you may
Nov 14, 2022 at 18:05 comment added Boffin May I use parts of your myscript under MIT license @RobertSiemer ?
Sep 15, 2022 at 15:32 comment added Robert Siemer @Seub "handles spaces in arguments" means enhanced getopt does not choke on --foo1 "bar1 bar2", which can feed bar1 bar2 as one option argument in the parsing loop. You can also write --foo1 bar1 --foo1 bar2 to feed arguments twice to --foo1’s parsing. – Otherwise the line you wrote equals bar2 bar4 bar5 --foo1 bar1 --foo2 bar3. Your wish of interpreting it different is uncommon, incompatible with OPs wish to mix arguments and options and not supported by enhanced getopt (nor any other solution I see here out of the box).
Sep 7, 2022 at 23:03 comment added Seub "handles spaces": It doesn't handle several arguments separated by spaces, right? I've been using your script but my use case is something like --foo1 bar1 bar2 --foo2 bar3 bar4 bar5. In this example foo1 and foo2 are options that can have arbitrarily many arguments separated by spaces. Would it be easy to support that?
Mar 28, 2022 at 16:18 history edited Robert Siemer CC BY-SA 4.0
copyedit
Mar 25, 2022 at 5:28 history edited Daniel Kaplan CC BY-SA 4.0
added 40 characters in body
Nov 10, 2021 at 23:06 comment added vaeVictis @RobertSiemer I just broke your 666 upvotes record, with my upvote :-D Very well explained. Thanks.
Jul 28, 2021 at 11:35 comment added YvesLeBorg @jjj brew install gnu-getopt ... and dont forget the small print about your path.
Mar 4, 2021 at 0:11 history edited Robert Siemer CC BY-SA 4.0
reinstate and improve _how_ this answer compares to the others and _why_ it is better
Mar 3, 2021 at 23:44 history rollback Robert Siemer
Rollback to Revision 39
Dec 8, 2020 at 13:01 history edited Inanc Gumus CC BY-SA 4.0
removed unnecessary header
Aug 19, 2019 at 23:35 comment added Robert Siemer @JaySomedon man getopt works on the command line and pretty good with Google. Go for section 1, i.e. getopt(1), because getopt(3) is the C function you talk about.
Jul 14, 2019 at 16:34 comment added Jay Somedon Is there link to doc on getopt command? All I see after googling getopt is about the c funciton not the terminal command..
May 15, 2019 at 12:06 history edited Robert Siemer CC BY-SA 4.0
improve comparison with top-voted answer
May 15, 2019 at 11:58 history edited Robert Siemer CC BY-SA 4.0
improve comparison with top-voted answer
May 15, 2019 at 11:49 comment added Robert Siemer @BenjaminW. The top-voted answer covers two self parsing solutions and getopts. The former don’t do combined short options, the latter doesn’t parse options after non-option arguments.
May 15, 2019 at 10:44 history edited Robert Siemer CC BY-SA 4.0
improving comments
May 12, 2019 at 16:20 review Suggested edits
May 12, 2019 at 20:18
Apr 11, 2019 at 2:24 comment added Robert Siemer @jjj footnote 1 covers OS X. – For OS X out-of-the-box solution check other questions and answers. Or to be honest: for real programming don’t use bash. ;-)
Apr 10, 2019 at 13:12 comment added jjj Note this doesn't work on Mac at least up to the current 10.14.3. The getopt that ships is BSD getopt from 1999...
Jan 20, 2019 at 14:50 history edited Robert Siemer CC BY-SA 4.0
rendered backticks safe in string
Aug 9, 2018 at 18:10 history edited BallpointBen CC BY-SA 4.0
added 30 characters in body
Jul 23, 2018 at 15:15 comment added Benjamin W. I don't know about the timelines of the different answers, but the top answer does cover -vfd style short options by way of the getopts built-in.
Jul 10, 2018 at 22:05 history edited Robert Siemer CC BY-SA 4.0
improve script by including errexit and fixing bugs detected by that option
Oct 24, 2017 at 19:37 history edited Robert Siemer CC BY-SA 3.0
improve script comment
Oct 24, 2017 at 19:31 history edited Robert Siemer CC BY-SA 3.0
improve script comment
Aug 20, 2017 at 12:49 history edited Robert Siemer CC BY-SA 3.0
copyediting
Aug 14, 2017 at 11:48 history edited Robert Siemer CC BY-SA 3.0
included hint for OS X on how to post-install enhanced getopt
Jul 27, 2017 at 9:23 history edited Robert Siemer CC BY-SA 3.0
add equal-sign for option arguments for clarity (that the option eats an argument)
Jun 30, 2017 at 16:28 history rollback Robert Siemer
Rollback to Revision 26
Jun 30, 2017 at 14:24 history edited abalter CC BY-SA 3.0
I ran the test specified on CentOS 7 and the test failed. So THAT linux distro does NOT have the new version.
May 23, 2017 at 12:10 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Apr 27, 2017 at 14:02 comment added Kaushal Modi I believe that the only caveat with getopt is that it cannot be used conveniently in wrapper scripts where one might have few options specific to the wrapper script, and then pass the non-wrapper-script options to the wrapped executable, intact. Let's say I have a grep wrapper called mygrep and I have an option --foo specific to mygrep, then I cannot do mygrep --foo -A 2, and have the -A 2 passed automatically to grep; I need to do mygrep --foo -- -A 2. Here is my implementation on top of your solution.
Mar 18, 2017 at 6:14 history edited bignose CC BY-SA 3.0
Use recommended ‘$()’ for command substitution.
Jan 12, 2017 at 2:00 comment added johncip Thanks for this. Just confirmed from the feature table at en.wikipedia.org/wiki/Getopts, if you need support for long options, and you're not on Solaris, getopt is the way to go.
Dec 20, 2016 at 17:21 history edited Robert Siemer CC BY-SA 3.0
added 41 characters in body
Dec 20, 2016 at 13:06 history edited Robert Siemer CC BY-SA 3.0
added 189 characters in body
Dec 17, 2016 at 18:24 history edited Robert Siemer CC BY-SA 3.0
edited body
Dec 17, 2016 at 1:38 history edited Robert Siemer CC BY-SA 3.0
added 43 characters in body
Dec 16, 2016 at 10:58 history edited Robert Siemer CC BY-SA 3.0
added 121 characters in body
Dec 15, 2016 at 2:38 history edited Robert Siemer CC BY-SA 3.0
added 5 characters in body
Dec 14, 2016 at 10:52 history edited Robert Siemer CC BY-SA 3.0
deleted 59 characters in body
Dec 9, 2016 at 16:36 history edited Robert Siemer CC BY-SA 3.0
added 240 characters in body
Nov 26, 2016 at 21:39 history edited Undo CC BY-SA 3.0
deleted 12 characters in body
Nov 4, 2016 at 10:31 history rollback Robert Siemer
Rollback to Revision 13
Nov 4, 2016 at 2:13 history edited Freiheit CC BY-SA 3.0
removed "ignorance" remarks
Oct 26, 2016 at 12:09 history edited Robert Siemer CC BY-SA 3.0
use proper (but bad looking :-) arithmetic operations instead of string ops (which did the same, though)
Oct 12, 2016 at 9:33 review Suggested edits
Oct 12, 2016 at 12:34
Oct 2, 2016 at 6:18 history edited Robert Siemer CC BY-SA 3.0
added 11 characters in body
Oct 2, 2016 at 6:13 history edited Robert Siemer CC BY-SA 3.0
added 11 characters in body
Aug 26, 2016 at 1:21 history edited Robert Siemer CC BY-SA 3.0
edited body
Aug 18, 2016 at 9:20 history edited Robert Siemer CC BY-SA 3.0
added 80 characters in body
Apr 16, 2016 at 15:04 history edited Robert Siemer CC BY-SA 3.0
added 465 characters in body
Dec 23, 2015 at 10:44 history edited Robert Siemer CC BY-SA 3.0
added 72 characters in body
Oct 20, 2015 at 23:02 history edited Robert Siemer CC BY-SA 3.0
Changed my example to exactly match the OPs question.
Sep 16, 2015 at 9:53 history edited Robert Siemer CC BY-SA 3.0
clear up test output
Jul 21, 2015 at 23:15 history edited Robert Siemer CC BY-SA 3.0
added example
Apr 21, 2015 at 7:21 history edited Robert Siemer CC BY-SA 3.0
added 194 characters in body
Apr 20, 2015 at 18:36 history edited seh CC BY-SA 3.0
Minor copyediting.
Apr 20, 2015 at 17:47 history answered Robert Siemer CC BY-SA 3.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /