`getopts` is a shell built-in used to parse command line options, while `getopt` is its non-built-in counterpart.
Questions tagged [getopts]
113 questions
69
votes
5 answers
getopt, getopts or manual parsing - what to use when I want to support both short and long options?
Currently I'm writing a Bash script which has the following requirements:
it should run on a wide variety of Unix/Linux platforms
it should support both short and (GNU) long options
I know that getopts would be the preferred way in terms of…
helpermethod
- 1,952
- 2
- 19
- 25
25
votes
6 answers
How can I detect that no options were passed with getopts?
I have this code -
#getoptDemo.sh
usage()
{
echo "usage: options:"
}
while getopts wlh: option
do
case $option in
(w)
name='1';;
(l)
name='2';;
…
Hussain Tamboli
- 1,085
- 3
- 14
- 21
25
votes
4 answers
How do I handle switches in a shell script?
Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments thereafter?
user394
- 14,194
- 21
- 66
- 93
16
votes
2 answers
What is the purpose of the very first character of the option-string of getopts being a : (colon)?
In option string when using getopts, from http://wiki.bash-hackers.org/howto/getopts_tutorial
If the very first character of the option-string is a : (colon), which would normally be nonsense because there's no
option letter preceding it, …
Tim
- 98,580
- 191
- 570
- 977
16
votes
1 answer
Using getopts to parse options after a non-option argument
I have a bash script as below in a file nepleaks_upd.sh, that I want to run as ./nepleaks_upd.sh bootstrap --branch off. Couldn't make it to take --branch , but what it works with is ./nepleaks_upd.sh bootstrap -b off.
usage() { echo "Usage: $0…
prayagupa
- 4,797
- 12
- 32
- 38
15
votes
1 answer
Provide two arguments to one option using getopts
In below code when I give option r then getopts requires one arguments:
while getopts ":hr::l:" opt; do
case $opt in
r ) echo "Run Numbers - argument = $OPTARG " ;;
l ) echo "Latency range - argument = $OPTARG" ;;
h )…
ramkrishna
- 275
- 1
- 3
- 7
13
votes
1 answer
Bash getopts, mandatory arguments
I am working with a pretty simple bash script, but I am facing a problem which I can't resolve:
I have myscript.sh with three parameters, -u, -h, and -p.
Both -u and -h are mandatory, needed for the script to run.
What I would like to do is, if…
Learner33
- 133
- 1
- 1
- 5
13
votes
4 answers
Bash function with `getopts` only works the first time it's run
I defined the function f in Bash based on the example here (under "An option with an argument"):
f () {
while getopts ":a:" opt; do
case $opt in
a)
echo "-a was triggered, Parameter: $OPTARG" >&2
;;
\?)
echo…
shadowtalker
- 1,198
- 2
- 11
- 23
10
votes
2 answers
How to deal with end of options -- in getopts
I use getopts to parse arguments in bash scripts as
while getopts ":hd:" opt; do
case $opt in
d ) echo "directory = $OPTARG"; mydir="$OPTARG"; shift $((OPTIND-1)); OPTIND=1 ;;
h ) helptext
graceful_exit ;;
* ) usage
…
highsciguy
- 2,534
- 4
- 21
- 29
10
votes
2 answers
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…
abalter
- 261
- 2
- 9
10
votes
3 answers
Bash: Why is eval and shift used in a script that parses command line arguments?
As I was looking this answer https://stackoverflow.com/a/11065196/4706711 in order to figure out on how to use parameters like --something or -s some questions rised regarding the answer's script :
#!/bin/bash
TEMP=`getopt -o ab:c:: --long…
Dimitrios Desyllas
- 979
- 3
- 11
- 27
9
votes
8 answers
Execute default option when no options are specified in getopts
I followed the tutorial here to learn how to use getopts. I'm able to execute all the options properly provided by the user. But now I want to execute a default option when none of the options are provided.
For e.g:
while getopts ":hr" opt; do
…
Abhimanyu Saharan
- 853
- 4
- 15
- 24
8
votes
5 answers
bash getopts, short options only, all require values, own validation
I'm trying to build up a shell script that accepts various options and getopts seems like a good solution as it can handle the variable ordering of the options and arguments (I think!).
I'll only be using short options and each short options will…
batfastad
- 1,035
- 2
- 13
- 24
8
votes
3 answers
How to specify -? option with GNU getopt
When parsing command line arguments with GNU getopt command, how do I (if possible) do recognize -? as another option? Is there a way to escape it in the opstring?
Martín Schonaker
- 265
- 3
- 6
7
votes
2 answers
Getopts option processing, Is it possible to add a non hyphenated [FILE]?
I'm using getopts for all of my scripts that require advanced option parsing, and It's worked great with dash. I'm familiar with the standard basic getopts usage, consisting of [-x] and [-x OPTION].
Is it possible to parse options like this?
…
J. M. Becker
- 4,831
- 3
- 28
- 40