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
2518
votes
41 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…
Redwood
  • 66,744
  • 41
  • 126
  • 187
555
votes
8 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
  • 41,599
  • 58
  • 163
  • 268
524
votes
32 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
  • 35,729
  • 29
  • 78
  • 113
76
votes
11 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
  • 989
  • 1
  • 7
  • 11
60
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
  • 10,736
  • 5
  • 44
  • 57
59
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
  • 773
  • 2
  • 6
  • 12
58
votes
15 answers

Optional option argument with getopts

while getopts "hd:R:" arg; do case $arg in h) echo "usage" ;; d) dir=$OPTARG ;; R) if [[ $OPTARG =~ ^[0-9]+$ ]];then level=$OPTARG else level=1 fi ;; \?) echo…
iverson
  • 1,003
  • 2
  • 11
  • 14
52
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
  • 585
  • 1
  • 6
  • 7
37
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
  • 13,045
  • 22
  • 71
  • 107
37
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
  • 945
  • 1
  • 10
  • 27
22
votes
1 answer

Shell Script Argument without value

I'm creating a very simple sh file to do something, but I want to pass an argument without a value, for example: ./fuim -l But I receive the following message: ./fuim: option requires an argument -- l If I pass a random value, like ./fuim -l 1, it…
Juliano A. Felipe
  • 574
  • 1
  • 7
  • 15
20
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
  • 343
  • 1
  • 3
  • 8
19
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
  • 201
  • 1
  • 2
  • 4
18
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,926
  • 6
  • 31
  • 34
16
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
  • 263
  • 1
  • 4
  • 10
1
2 3
29 30