Which is the more standard package, getopt or getopts (with an “s”)?












6















The Linux foundation list of standard utilities includes getopts but not getopt. Similar for the Open Group list of Posix utilities.



Meanwhile, Wikipedia's list of standard Unix Commands includes getopt but not getopts. Similarly, the Windows Subsystem for Linux (based on Ubuntu based on Debian) also includes getopt but not getopts (and it is the GNU Enhanced version).



balter@spectre:~$ which getopt
/usr/bin/getopt
balter@spectre:~$ getopt -V
getopt from util-linux 2.27.1
balter@spectre:~$ which getopts
balter@spectre:~$


So if I want to pick one that I can be the most confident that anyone using one of the more standard Linux distros (e.g. Debian, Red Hat, Ubuntu, Fedora, CentOS, etc.), which should I pick?



Note:



thanks to Michael and Muru for explaining about builtin vs executable. I had just stumbled across this as well which lists bash builtins.










share|improve this question




















  • 5





    getopts is a shell built-in (as both of your sources identify), not an executable. It's in every POSIX shell, including the one from WSL Ubuntu. What are you actually trying to do with it? It's not impossible that there are cases where it matters which you use, but there's not enough information here to say.

    – Michael Homer
    May 14 '18 at 4:54













  • Wanting to use either getopt or getopts at all is a warning sign that your shell script is probably complicated enough that you should consider rewriting it in a better programming language, e.g. Perl, Python, Ruby, even PHP.

    – zwol
    May 14 '18 at 15:44











  • @zwol -- I could throw it back at you and say your statement indicates you aren't a skilled bash programmer ;) I work in a field where we pipeline other command line programs, and run the pipelines on a cluster. Using a bash script for this is by far the most efficient method. I've had great success with using getopts in this application. I use Python when it is the better tool.

    – abalter
    May 14 '18 at 16:38
















6















The Linux foundation list of standard utilities includes getopts but not getopt. Similar for the Open Group list of Posix utilities.



Meanwhile, Wikipedia's list of standard Unix Commands includes getopt but not getopts. Similarly, the Windows Subsystem for Linux (based on Ubuntu based on Debian) also includes getopt but not getopts (and it is the GNU Enhanced version).



balter@spectre:~$ which getopt
/usr/bin/getopt
balter@spectre:~$ getopt -V
getopt from util-linux 2.27.1
balter@spectre:~$ which getopts
balter@spectre:~$


So if I want to pick one that I can be the most confident that anyone using one of the more standard Linux distros (e.g. Debian, Red Hat, Ubuntu, Fedora, CentOS, etc.), which should I pick?



Note:



thanks to Michael and Muru for explaining about builtin vs executable. I had just stumbled across this as well which lists bash builtins.










share|improve this question




















  • 5





    getopts is a shell built-in (as both of your sources identify), not an executable. It's in every POSIX shell, including the one from WSL Ubuntu. What are you actually trying to do with it? It's not impossible that there are cases where it matters which you use, but there's not enough information here to say.

    – Michael Homer
    May 14 '18 at 4:54













  • Wanting to use either getopt or getopts at all is a warning sign that your shell script is probably complicated enough that you should consider rewriting it in a better programming language, e.g. Perl, Python, Ruby, even PHP.

    – zwol
    May 14 '18 at 15:44











  • @zwol -- I could throw it back at you and say your statement indicates you aren't a skilled bash programmer ;) I work in a field where we pipeline other command line programs, and run the pipelines on a cluster. Using a bash script for this is by far the most efficient method. I've had great success with using getopts in this application. I use Python when it is the better tool.

    – abalter
    May 14 '18 at 16:38














6












6








6


1






The Linux foundation list of standard utilities includes getopts but not getopt. Similar for the Open Group list of Posix utilities.



Meanwhile, Wikipedia's list of standard Unix Commands includes getopt but not getopts. Similarly, the Windows Subsystem for Linux (based on Ubuntu based on Debian) also includes getopt but not getopts (and it is the GNU Enhanced version).



balter@spectre:~$ which getopt
/usr/bin/getopt
balter@spectre:~$ getopt -V
getopt from util-linux 2.27.1
balter@spectre:~$ which getopts
balter@spectre:~$


So if I want to pick one that I can be the most confident that anyone using one of the more standard Linux distros (e.g. Debian, Red Hat, Ubuntu, Fedora, CentOS, etc.), which should I pick?



Note:



thanks to Michael and Muru for explaining about builtin vs executable. I had just stumbled across this as well which lists bash builtins.










share|improve this question
















The Linux foundation list of standard utilities includes getopts but not getopt. Similar for the Open Group list of Posix utilities.



Meanwhile, Wikipedia's list of standard Unix Commands includes getopt but not getopts. Similarly, the Windows Subsystem for Linux (based on Ubuntu based on Debian) also includes getopt but not getopts (and it is the GNU Enhanced version).



balter@spectre:~$ which getopt
/usr/bin/getopt
balter@spectre:~$ getopt -V
getopt from util-linux 2.27.1
balter@spectre:~$ which getopts
balter@spectre:~$


So if I want to pick one that I can be the most confident that anyone using one of the more standard Linux distros (e.g. Debian, Red Hat, Ubuntu, Fedora, CentOS, etc.), which should I pick?



Note:



thanks to Michael and Muru for explaining about builtin vs executable. I had just stumbled across this as well which lists bash builtins.







linux posix getopts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 14 '18 at 5:35







abalter

















asked May 14 '18 at 4:48









abalterabalter

1415




1415








  • 5





    getopts is a shell built-in (as both of your sources identify), not an executable. It's in every POSIX shell, including the one from WSL Ubuntu. What are you actually trying to do with it? It's not impossible that there are cases where it matters which you use, but there's not enough information here to say.

    – Michael Homer
    May 14 '18 at 4:54













  • Wanting to use either getopt or getopts at all is a warning sign that your shell script is probably complicated enough that you should consider rewriting it in a better programming language, e.g. Perl, Python, Ruby, even PHP.

    – zwol
    May 14 '18 at 15:44











  • @zwol -- I could throw it back at you and say your statement indicates you aren't a skilled bash programmer ;) I work in a field where we pipeline other command line programs, and run the pipelines on a cluster. Using a bash script for this is by far the most efficient method. I've had great success with using getopts in this application. I use Python when it is the better tool.

    – abalter
    May 14 '18 at 16:38














  • 5





    getopts is a shell built-in (as both of your sources identify), not an executable. It's in every POSIX shell, including the one from WSL Ubuntu. What are you actually trying to do with it? It's not impossible that there are cases where it matters which you use, but there's not enough information here to say.

    – Michael Homer
    May 14 '18 at 4:54













  • Wanting to use either getopt or getopts at all is a warning sign that your shell script is probably complicated enough that you should consider rewriting it in a better programming language, e.g. Perl, Python, Ruby, even PHP.

    – zwol
    May 14 '18 at 15:44











  • @zwol -- I could throw it back at you and say your statement indicates you aren't a skilled bash programmer ;) I work in a field where we pipeline other command line programs, and run the pipelines on a cluster. Using a bash script for this is by far the most efficient method. I've had great success with using getopts in this application. I use Python when it is the better tool.

    – abalter
    May 14 '18 at 16:38








5




5





getopts is a shell built-in (as both of your sources identify), not an executable. It's in every POSIX shell, including the one from WSL Ubuntu. What are you actually trying to do with it? It's not impossible that there are cases where it matters which you use, but there's not enough information here to say.

– Michael Homer
May 14 '18 at 4:54







getopts is a shell built-in (as both of your sources identify), not an executable. It's in every POSIX shell, including the one from WSL Ubuntu. What are you actually trying to do with it? It's not impossible that there are cases where it matters which you use, but there's not enough information here to say.

– Michael Homer
May 14 '18 at 4:54















Wanting to use either getopt or getopts at all is a warning sign that your shell script is probably complicated enough that you should consider rewriting it in a better programming language, e.g. Perl, Python, Ruby, even PHP.

– zwol
May 14 '18 at 15:44





Wanting to use either getopt or getopts at all is a warning sign that your shell script is probably complicated enough that you should consider rewriting it in a better programming language, e.g. Perl, Python, Ruby, even PHP.

– zwol
May 14 '18 at 15:44













@zwol -- I could throw it back at you and say your statement indicates you aren't a skilled bash programmer ;) I work in a field where we pipeline other command line programs, and run the pipelines on a cluster. Using a bash script for this is by far the most efficient method. I've had great success with using getopts in this application. I use Python when it is the better tool.

– abalter
May 14 '18 at 16:38





@zwol -- I could throw it back at you and say your statement indicates you aren't a skilled bash programmer ;) I work in a field where we pipeline other command line programs, and run the pipelines on a cluster. Using a bash script for this is by far the most efficient method. I've had great success with using getopts in this application. I use Python when it is the better tool.

– abalter
May 14 '18 at 16:38










2 Answers
2






active

oldest

votes


















13














which is the wrong tool. getopts is usually also a builtin:




Since getopts affects the current shell execution environment, it is
generally provided as a shell regular built-in.




~ for sh in dash ksh bash zsh; do "$sh" -c 'printf "%s in %sn" "$(type getopts)" "$0"'; done
getopts is a shell builtin in dash
getopts is a shell builtin in ksh
getopts is a shell builtin in bash
getopts is a shell builtin in zsh


If you're using a shell script, you can safely depend on getopts. There might be other reasons to favour one or the other, but getopts is standard.



See also: Why not use "which"? What to use then?






share|improve this answer





















  • 1





    getopt was dropped a while ago in favor of getopts. However, the new GNU-enhanced getopt seems to have improved over getopts. But if getopts is the Posix standard, then I should stick to that for the time being.

    – abalter
    May 14 '18 at 5:37



















1














I too would prefer getopts over getopt for the following reasons:




getopt Cons




  1. External utility

  2. Cannot handle empty argument string or arguments with embedded whitespace in traditional version



getopts Pros




  1. Works in any POSIX shell and is portable

  2. Works well with -a -b as well as -ab






share|improve this answer


























  • Very clear. Thanks! Especially the information about -a -b === -ab

    – abalter
    May 17 '18 at 15:06











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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
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%2f443617%2fwhich-is-the-more-standard-package-getopt-or-getopts-with-an-s%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









13














which is the wrong tool. getopts is usually also a builtin:




Since getopts affects the current shell execution environment, it is
generally provided as a shell regular built-in.




~ for sh in dash ksh bash zsh; do "$sh" -c 'printf "%s in %sn" "$(type getopts)" "$0"'; done
getopts is a shell builtin in dash
getopts is a shell builtin in ksh
getopts is a shell builtin in bash
getopts is a shell builtin in zsh


If you're using a shell script, you can safely depend on getopts. There might be other reasons to favour one or the other, but getopts is standard.



See also: Why not use "which"? What to use then?






share|improve this answer





















  • 1





    getopt was dropped a while ago in favor of getopts. However, the new GNU-enhanced getopt seems to have improved over getopts. But if getopts is the Posix standard, then I should stick to that for the time being.

    – abalter
    May 14 '18 at 5:37
















13














which is the wrong tool. getopts is usually also a builtin:




Since getopts affects the current shell execution environment, it is
generally provided as a shell regular built-in.




~ for sh in dash ksh bash zsh; do "$sh" -c 'printf "%s in %sn" "$(type getopts)" "$0"'; done
getopts is a shell builtin in dash
getopts is a shell builtin in ksh
getopts is a shell builtin in bash
getopts is a shell builtin in zsh


If you're using a shell script, you can safely depend on getopts. There might be other reasons to favour one or the other, but getopts is standard.



See also: Why not use "which"? What to use then?






share|improve this answer





















  • 1





    getopt was dropped a while ago in favor of getopts. However, the new GNU-enhanced getopt seems to have improved over getopts. But if getopts is the Posix standard, then I should stick to that for the time being.

    – abalter
    May 14 '18 at 5:37














13












13








13







which is the wrong tool. getopts is usually also a builtin:




Since getopts affects the current shell execution environment, it is
generally provided as a shell regular built-in.




~ for sh in dash ksh bash zsh; do "$sh" -c 'printf "%s in %sn" "$(type getopts)" "$0"'; done
getopts is a shell builtin in dash
getopts is a shell builtin in ksh
getopts is a shell builtin in bash
getopts is a shell builtin in zsh


If you're using a shell script, you can safely depend on getopts. There might be other reasons to favour one or the other, but getopts is standard.



See also: Why not use "which"? What to use then?






share|improve this answer















which is the wrong tool. getopts is usually also a builtin:




Since getopts affects the current shell execution environment, it is
generally provided as a shell regular built-in.




~ for sh in dash ksh bash zsh; do "$sh" -c 'printf "%s in %sn" "$(type getopts)" "$0"'; done
getopts is a shell builtin in dash
getopts is a shell builtin in ksh
getopts is a shell builtin in bash
getopts is a shell builtin in zsh


If you're using a shell script, you can safely depend on getopts. There might be other reasons to favour one or the other, but getopts is standard.



See also: Why not use "which"? What to use then?







share|improve this answer














share|improve this answer



share|improve this answer








edited May 17 '18 at 10:59









Jeff Schaller

42.9k1159137




42.9k1159137










answered May 14 '18 at 4:55









murumuru

1




1








  • 1





    getopt was dropped a while ago in favor of getopts. However, the new GNU-enhanced getopt seems to have improved over getopts. But if getopts is the Posix standard, then I should stick to that for the time being.

    – abalter
    May 14 '18 at 5:37














  • 1





    getopt was dropped a while ago in favor of getopts. However, the new GNU-enhanced getopt seems to have improved over getopts. But if getopts is the Posix standard, then I should stick to that for the time being.

    – abalter
    May 14 '18 at 5:37








1




1





getopt was dropped a while ago in favor of getopts. However, the new GNU-enhanced getopt seems to have improved over getopts. But if getopts is the Posix standard, then I should stick to that for the time being.

– abalter
May 14 '18 at 5:37





getopt was dropped a while ago in favor of getopts. However, the new GNU-enhanced getopt seems to have improved over getopts. But if getopts is the Posix standard, then I should stick to that for the time being.

– abalter
May 14 '18 at 5:37













1














I too would prefer getopts over getopt for the following reasons:




getopt Cons




  1. External utility

  2. Cannot handle empty argument string or arguments with embedded whitespace in traditional version



getopts Pros




  1. Works in any POSIX shell and is portable

  2. Works well with -a -b as well as -ab






share|improve this answer


























  • Very clear. Thanks! Especially the information about -a -b === -ab

    – abalter
    May 17 '18 at 15:06
















1














I too would prefer getopts over getopt for the following reasons:




getopt Cons




  1. External utility

  2. Cannot handle empty argument string or arguments with embedded whitespace in traditional version



getopts Pros




  1. Works in any POSIX shell and is portable

  2. Works well with -a -b as well as -ab






share|improve this answer


























  • Very clear. Thanks! Especially the information about -a -b === -ab

    – abalter
    May 17 '18 at 15:06














1












1








1







I too would prefer getopts over getopt for the following reasons:




getopt Cons




  1. External utility

  2. Cannot handle empty argument string or arguments with embedded whitespace in traditional version



getopts Pros




  1. Works in any POSIX shell and is portable

  2. Works well with -a -b as well as -ab






share|improve this answer















I too would prefer getopts over getopt for the following reasons:




getopt Cons




  1. External utility

  2. Cannot handle empty argument string or arguments with embedded whitespace in traditional version



getopts Pros




  1. Works in any POSIX shell and is portable

  2. Works well with -a -b as well as -ab







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 17 at 18:22









dessert

1,258623




1,258623










answered May 17 '18 at 11:19









ArushixArushix

1,0418




1,0418













  • Very clear. Thanks! Especially the information about -a -b === -ab

    – abalter
    May 17 '18 at 15:06



















  • Very clear. Thanks! Especially the information about -a -b === -ab

    – abalter
    May 17 '18 at 15:06

















Very clear. Thanks! Especially the information about -a -b === -ab

– abalter
May 17 '18 at 15:06





Very clear. Thanks! Especially the information about -a -b === -ab

– abalter
May 17 '18 at 15:06


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f443617%2fwhich-is-the-more-standard-package-getopt-or-getopts-with-an-s%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀