Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • This is almost certainly a case of the XY Problem. Would you mind editing your question to explain what you're ultimately trying to accomplish? (Or if this a case of pure intellectual interest, edit your question to specify that.) Commented Mar 31, 2016 at 23:39
  • @Wildcard added a specific use-case, but I'm asking primarily out of curiosity. Commented Apr 1, 2016 at 0:10
  • as @Wildcard says, the only safe way to have optional arguments is to use getopts or similar (e.g. /usr/bin/getopt from the util-linux package if you want to support --long options as well as short). Commented Apr 1, 2016 at 22:37
  • But beware of using getopt in scripts. It's not even part of bash, so it introduces an external dependency. Super un-portable. @cas Commented Apr 1, 2016 at 22:46
  • Which is mostly why I mentioned where it came from. The other reason is that other, non-util-linux, versions of getopt are known to be buggy and unsafe to use. getopt from util-linux is available for and/or can be compiled for other unixes. if you want/need --long options in shell scripts, it's really the only choice available. BTW, even SC's getopts_long shell function uses perl and the Getopt::Long perl module, which are also external dependencies. Commented Apr 1, 2016 at 22:54