Which is the more standard package, getopt or getopts (with an “s”)?
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
add a comment |
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
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 eithergetopt
orgetopts
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
add a comment |
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
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
linux posix getopts
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 eithergetopt
orgetopts
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
add a comment |
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 eithergetopt
orgetopts
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
add a comment |
2 Answers
2
active
oldest
votes
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?
1
getopt
was dropped a while ago in favor ofgetopts
. However, the new GNU-enhancedgetopt
seems to have improved overgetopts
. But ifgetopts
is the Posix standard, then I should stick to that for the time being.
– abalter
May 14 '18 at 5:37
add a comment |
I too would prefer getopts
over getopt
for the following reasons:
getopt
Cons
- External utility
- Cannot handle empty argument string or arguments with embedded whitespace in traditional version
getopts
Pros
- Works in any POSIX shell and is portable
- Works well with
-a -b
as well as-ab
Very clear. Thanks! Especially the information about-a -b === -ab
– abalter
May 17 '18 at 15:06
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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?
1
getopt
was dropped a while ago in favor ofgetopts
. However, the new GNU-enhancedgetopt
seems to have improved overgetopts
. But ifgetopts
is the Posix standard, then I should stick to that for the time being.
– abalter
May 14 '18 at 5:37
add a comment |
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?
1
getopt
was dropped a while ago in favor ofgetopts
. However, the new GNU-enhancedgetopt
seems to have improved overgetopts
. But ifgetopts
is the Posix standard, then I should stick to that for the time being.
– abalter
May 14 '18 at 5:37
add a comment |
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?
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?
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 ofgetopts
. However, the new GNU-enhancedgetopt
seems to have improved overgetopts
. But ifgetopts
is the Posix standard, then I should stick to that for the time being.
– abalter
May 14 '18 at 5:37
add a comment |
1
getopt
was dropped a while ago in favor ofgetopts
. However, the new GNU-enhancedgetopt
seems to have improved overgetopts
. But ifgetopts
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
add a comment |
I too would prefer getopts
over getopt
for the following reasons:
getopt
Cons
- External utility
- Cannot handle empty argument string or arguments with embedded whitespace in traditional version
getopts
Pros
- Works in any POSIX shell and is portable
- Works well with
-a -b
as well as-ab
Very clear. Thanks! Especially the information about-a -b === -ab
– abalter
May 17 '18 at 15:06
add a comment |
I too would prefer getopts
over getopt
for the following reasons:
getopt
Cons
- External utility
- Cannot handle empty argument string or arguments with embedded whitespace in traditional version
getopts
Pros
- Works in any POSIX shell and is portable
- Works well with
-a -b
as well as-ab
Very clear. Thanks! Especially the information about-a -b === -ab
– abalter
May 17 '18 at 15:06
add a comment |
I too would prefer getopts
over getopt
for the following reasons:
getopt
Cons
- External utility
- Cannot handle empty argument string or arguments with embedded whitespace in traditional version
getopts
Pros
- Works in any POSIX shell and is portable
- Works well with
-a -b
as well as-ab
I too would prefer getopts
over getopt
for the following reasons:
getopt
Cons
- External utility
- Cannot handle empty argument string or arguments with embedded whitespace in traditional version
getopts
Pros
- Works in any POSIX shell and is portable
- Works well with
-a -b
as well as-ab
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
orgetopts
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