Questions tagged [getopts]

getopts is a Bourne/POSIX shell builtin for parsing command-line options, available in ash, bash, dash, ksh, zsh, ... on Linux and other Unix systems.

getopts is a shell builtin for parsing command-line options of the form -a where a is a letter or other character. It is available in all Bourne-style shells (Bourne, ash, bash, dash, ksh, zsh, ...) and defined by the POSIX standard.

Using getopts usually involves using a loop calling getopts OPTSTRING varname, in which each call to getopts will parse the next option, set varname with the option name and return success.

Example

#!/bin/sh

while getopts a:b flag
do
    case $flag in
        a) echo "a flag used, with value $OPTARG" ;;
        b) echo "b flag used" ;;
        ?) echo "Usage: PROGRAM [-a ARG1] [-b]"; exit 1 ;;
    esac
done
shift $((OPTIND - 1)) # remove parsed args from the arglist
echo remaining args: $*

Documentation

444 questions
3
votes
2 answers

Bash getopts command

I am following IBM's example from their website: (listing #5) http://www.ibm.com/developerworks/library/l-bash-parameters/index.html #!/bin/bash echo "OPTIND starts at $OPTIND" while getopts ":pq:" optname do case "$optname" in "p") …
spatara
  • 893
  • 4
  • 15
  • 28
3
votes
2 answers

getopts not working - bash

I am writing a bash script which accept parameters. I am using getopts to achieve it. #!/bin/bash while getopts ":a" opt; do case $opt in a) echo "-a was triggered!" >&2 ;; \?) echo "Invalid option: -$OPTARG" >&2 …
pahan
  • 2,445
  • 1
  • 28
  • 36
3
votes
2 answers

How do you use getopts?

what is the easiest, most straight forward, way to use getopts in bash script. if i have a script called: myscript and it CAN take the the arguments: -p -r -s -x if argument x then exit if argument p then echo "port 10" if argument s then add 2+2 if…
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
3
votes
2 answers

how to use getopt(s) as technique for passing in argument in bash

Can someone show me an example how to use getopts properly or any other technique that I would be able to pass in an argument? I am trying to write this in unix shell/bash. I am seeing there is getopt and getopts and not sure which is better to use.…
jdamae
  • 3,839
  • 16
  • 58
  • 78
3
votes
1 answer

Enable autocomplete for parsed arguments in getopts

I have a bash script that uses getopts to parse command line arguments. One of the arguments, -l is directed to an if statement that determines certain settings. Is it possible to have autocomplete work in the command line for entering the…
justinian482
  • 845
  • 2
  • 10
  • 18
3
votes
1 answer

zsh `getopts` OPTIND behavior not consistent with other shells (bash, sh)

The definition of $OPTIND in POSIX shell, bash are quite consistent and intuitive - it is the index of the next arg to be read. However, its behavior in zsh is quite puzzling, and I can't find document for it. Example: # ./test.sh: foo() { while…
KFL
  • 17,162
  • 17
  • 65
  • 89
3
votes
2 answers

getopts is not recognizing the specified options as valid

I wrote the following shellscript. I am trying to get inputs from the user using the getopts method. This is the getopts fragment of code that I wrote. #Define the help function function help(){ echo "Options:"; echo "-u Github username" …
Nicolas
  • 91
  • 2
  • 6
3
votes
1 answer

Using getopts with no argument for help output

Hi I'm creating a bash script which uses getopts. Now I want to create an "-h" parameter to get the help. But every time I have to give one argument to the parameter. Now test.sh -h test What I want test.sh -h help help help while getopts…
tso
  • 187
  • 4
  • 13
3
votes
1 answer

Bash long options/flags - how to do it?

I am trying to change my working script with getopts to getopt ( long flags ). Below i present my code which is working. getopts 'm:' mode modeValue=$OPTARG getopts 'p:' parameter parameterValue=$OPTARG getopts 'u:'…
adamos
  • 69
  • 8
3
votes
2 answers

Need to use getopts with one argument OR another

while getopts gpr: | gpd: name do case $name in g) echo " -g";; p) echo " -p";; r) echo " -r with '$OPTARG'";; d) echo " -d with '$OPTARG'";; esac done I know this is wrongly written. I wanted to write it that way so…
TheLibrarian
  • 1,540
  • 1
  • 11
  • 22
3
votes
3 answers

How to use mutually exclusive flags in your shell and add an optional argument flag ( stuck with getopts)

I am using a standard getopts logic. But I want to how I can make the options I offer- mutually exclusive. e.g. shell.sh -a SID shell.sh -b SID shell.sh -ab…
user1874594
  • 2,277
  • 1
  • 25
  • 49
3
votes
2 answers

How to allow non-optional arguments to be in front of optional arguments using getopts in Bash?

I know how to handle optional arguments using getopts in Bash #!/bin/bash while getopts ":a:p:drh" opt; do case "$opt" in a) echo $OPTARG;; esac done But if my script expects ./script.sh arg1 [options], how do I tell getopts to skip…
CppLearner
  • 16,273
  • 32
  • 108
  • 163
3
votes
2 answers

getopts called in function not picking up flags

I have the following script that I call from my .bash_profile: # Set directories based on current path __set_dirs() { currdir=`pwd` if [[ ${currdir} =~ "\/path\/to\/main\/(.*)\/working\/([a-z]+)(/?.*)" ]] then …
abeger
  • 6,766
  • 7
  • 41
  • 58
3
votes
1 answer

getopts is not parsing my command line options

I'm running bash version 4.2, and I'm trying to parse command line parameters with builtin command getopts, But getopts doesn't seem to parse it correctly, if -s wasn't the first parameter, it won't be parsed -s not parsed: %> ./getopt.sh aaa -s…
daisy
  • 22,498
  • 29
  • 129
  • 265
3
votes
2 answers

Why does my shell script fail on second attempt?

This script should accept a set of search terms and return a formatted URL to search google. $ ./google_search.sh albert einstein https://www.google.com/search?q=albert+einstein It does that just fine, so I decided to add an option to search a…
Jack Stout
  • 1,265
  • 3
  • 12
  • 25