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

386 questions
2162
votes
37 answers

How do I parse command line arguments in Bash?

Say, I have a script that gets called with this line: ./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile or this one: ./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile What's the accepted way of parsing this such that in each…
Lawrence Johnston
  • 58,608
  • 40
  • 113
  • 181
438
votes
31 answers

Using getopts to process long and short command line options

I wish to have long and short forms of command line options invoked using my shell script. I know that getopts can be used, but like in Perl, I have not been able to do the same with shell. Any ideas on how this can be done, so that I can use…
gagneet
  • 31,111
  • 28
  • 70
  • 99
397
votes
7 answers

An example of how to use getopts in bash

I want to call myscript file in this way: $ ./myscript -s 45 -p any_string or $ ./myscript -h #should display help $ ./myscript #should display help My requirements are: getopt here to get the input arguments check that -s exists, if not…
MOHAMED
  • 35,883
  • 48
  • 140
  • 238
64
votes
10 answers

Retrieving multiple arguments for a single option using getopts in Bash

I need help with getopts. I created a Bash script which looks like this when run: $ foo.sh -i env -d directory -s subdirectory -f file It works correctly when handling one argument from each flag. But when I invoke several arguments from each flag…
vegasbrianc
  • 799
  • 1
  • 6
  • 9
51
votes
3 answers

Using getopts inside a Bash function

I'd like to use getopts inside a function that I have defined in my .bash_profile. The idea is I'd like to pass in some flags to this function to alter its behavior. Here's the code: function t() { echo $* getopts "a:" OPTION echo…
Magnus
  • 9,320
  • 4
  • 38
  • 51
48
votes
1 answer

bash getopts with multiple and mandatory options

Is it possible to use getopts to process multiple options together? For example, myscript -iR or myscript -irv. Also, I have a situation where based on a condition script would need mandatory option. For example, if argument to script is a…
Ramesh Samane
  • 545
  • 1
  • 5
  • 7
47
votes
11 answers

Optional option argument with getopts

while getopts "hd:R:" arg; do case $arg in h) echo "usgae" ;; d) dir=$OPTARG ;; R) if [[ $OPTARG =~ ^[0-9]+$ ]];then level=$OPTARG else level=1 fi ;; \?) echo…
iverson
  • 853
  • 2
  • 10
  • 14
43
votes
9 answers

How can I use long options with the Bash getopts builtin?

I am trying to parse a -temp option with Bash getopts. I'm calling my script like this: ./myscript -temp /foo/bar/someFile Here is the code I'm using to parse the options. while getopts "temp:shots:o:" option; do case $option in temp)…
Hemang
  • 613
  • 2
  • 6
  • 12
32
votes
2 answers

parse arguments after getopts

I want to call a bash script like this $ ./scriptName -o -p -t something path/to/file This is as far as I get #!/bin/bash o=false p=false while getopts ":opt:" options do case $options in o ) opt1=true ;; p )…
speendo
  • 11,483
  • 19
  • 66
  • 100
31
votes
6 answers

Reading $OPTARG for optional flags?

I'd like to be able to accept both mandatory and optional flags in my script. Here's what I have so far. #!bin/bash while getopts ":a:b:cdef" opt; do case $opt in a ) APPLE="$OPTARG";; b ) BANANA="$OPTARG";; c )…
earth
  • 883
  • 1
  • 8
  • 26
15
votes
2 answers

How to handle shell getopts with parameter containing blank spaces

I'm looking for a way to handle arguments containing blank spaces that has to be parsed by shell getopts command. while getopts ":a:i:o:e:v:u:" arg do echo "ARG is: $arg" >> /tmp/submit.log case "$arg" in a) arg1="$OPTARG" ;; i)…
DrFalk3n
  • 4,756
  • 6
  • 29
  • 34
14
votes
2 answers

Best way to parse command line args in Bash?

After several days of research, I still can't figure out the best method for parsing cmdline args in a .sh script. According to my references the getopts cmd is the way to go since it "extracts and checks switches without disturbing the positional…
LogicalConfusion
  • 233
  • 1
  • 4
  • 10
14
votes
1 answer

How does the OPTIND variable work in the shell builtin getopts

My shell script is quite simple, as the following: while getopts "abc:" flag; do echo "$flag" $OPTIND $OPTARG done And I do some testing as the following: Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -abc CCC Blank a 1 b 1 c 3…
Blank
  • 183
  • 1
  • 2
  • 8
12
votes
2 answers

getopts won't call twice in a row?

For some reason the options work fine the first call of lib_progress_bar -c "@" -u "_" 0 100, but on the second call and beyond everything is default because it seems like getopts c:u:d:p:s:%:m: flag isn't true the second time around, or atleast the…
ParoX
  • 4,850
  • 19
  • 72
  • 138
11
votes
1 answer

How to use getopts option without argument at the end in bash

I want to use getopts in bash. The user should select his own options. most of the options gets arguments but there are 2 options that does not gets an argument. It works good when the option without the argument is at the middle of the command,…
Nissim
  • 113
  • 1
  • 1
  • 4
1
2 3
25 26