What is the behavior of `getopts` when it meets `--`?
Clash 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.
bash getopts
add a comment |Â
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.
bash getopts
add a comment |Â
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.
bash getopts
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.
bash getopts
edited Feb 25 at 19:11
asked Feb 25 at 19:02
Tim
22.7k64224401
22.7k64224401
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Feb 25 at 20:52
Stéphane Chazelas
280k53516847
280k53516847
answered Feb 25 at 19:08
Kusalananda
103k13202318
103k13202318
add a comment |Â
add a comment |Â
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
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
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
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
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