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
15
votes
2 answers

bash getopts multiple arguments or default value

So I have a question about get opts in bash. I want to get the value of the arguments if they are present but if they are not present to use a default value. So the script should take a directory and an integer but if they aren't specified then…
Greg
  • 737
  • 4
  • 13
  • 21
13
votes
1 answer

getopts checking for mutually exclusive arguments

I have a simple script (below) that has mutually exclusive arguments. The arguments for the script should be ./scriptname.sh -m|-d [-n], however, a user can run the script with ./scriptname.sh -m -d which is wrong. Question: how can I enforce that…
Chris Snow
  • 23,813
  • 35
  • 144
  • 309
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
  • 5,685
  • 23
  • 81
  • 152
11
votes
4 answers

Multiple option arguments using getopts (bash)

I am trying to process command line arguments using getopts in bash. One of the requirements is for the processing of an arbitrary number of option arguments (without the use of quotes). 1st example (only grabs the 1st argument) madcap:~/projects$…
user1117603
  • 421
  • 2
  • 5
  • 13
11
votes
5 answers

Add usage content in shell script without getopts

I have script where i need to display usage command in case user miss any mandatory information while executing script. Usage : Script -s -i -u -p -w -c With explanation…
San
  • 223
  • 1
  • 6
  • 15
10
votes
3 answers

Using --getopts to pick up whole word flags

Can getopts be used to pick up whole-word flags? Something as follows: while getopts ":abc --word" opt; do case ${opt} in a) SOMETHING ;; ... --word) echo "Do something else." ;; …
kid_x
  • 1,415
  • 1
  • 11
  • 31
9
votes
1 answer

Boolean cli flag using getopts in bash?

Is it possible to implement a boolean cli option using getopts in bash? Basically I want to do one thing if -x is specified and another if it is not.
foobar0100
  • 291
  • 1
  • 3
  • 9
9
votes
3 answers

Bug in parsing args with getopts in bash

I was trying to modify the bd script to use getopts. I am a newbie at bash scripting my script is while getopts ":hvis:d:" opt do ... done ... echo $somedirpath cd "$somedirpath" this runs fine when doing $ ./bd -v -i -s search or $ ./bd…
udiboy1209
  • 1,472
  • 1
  • 15
  • 33
8
votes
3 answers

Having getopts to show help if no options provided

I parsed some similar questions posted here but they aren't suitable for me. I've got this wonderful bash script which does some cool functions, here is the relevant section of the code: while getopts ":hhelpf:d:c:" ARGS; do case $ARGS in …
Omar
  • 309
  • 1
  • 2
  • 10
7
votes
2 answers

Why does getopts only work the first time?

Why does this option only work the first time it's used, then ignored every other time? It's like it's being reset when the option is not used. Here's my function: testopts() { local var="o false" while getopts "o" option; do case…
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
7
votes
1 answer

Command line arguments validation with GetOpts and mandatory parameters

I'm creating a basic script that should take 3 mandatory command line options and each one must be followed by a value. Like this: $ myscript.sh -u -p -f I'm trying to make sure the user is passing those exact 3…
donhector
  • 875
  • 1
  • 10
  • 21
7
votes
1 answer

Required option getopts linux

I have to write a bash script: schedsim.sh [-h] [-c #CPUs ] -i pathfile h and c are optional options. i is required, when run script if it doesn't have i option -> error message. How to make a required option in getopts? Thanks! another…
rocketmanu
  • 137
  • 2
  • 3
  • 10
6
votes
1 answer

How to combine getopts and positional parameters in bash?

I want to use both getopts and positional parameters, but if I pass in a positional parameter to the program the getopts get lost. directory=$1 while getopts l: flag; do case "$flag" in l) level=$OPTARG;; esac done if [ -n "$level" ];…
bntzio
  • 1,274
  • 14
  • 27
6
votes
2 answers

Mass arguments (operands) at first place in command line argument passing

I use following lines (hope this is best practice if not correct me please) to handle command line options: #!/usr/bin/bash read -r -d '' HELP <
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
5
votes
4 answers

getopts ignores all arguments if an unnamed argument precedes named arguments ("./foo unnamed -n named")

I'm trying to understand why getopts seems to ignore all arguments if an "unnnamed" argument precedes any named arguments. Using an example from http://wiki.bash-hackers.org/howto/getopts_tutorial, #!/bin/bash while getopts ":a" opt; do case…
gamen
  • 987
  • 6
  • 12
1
2
3
29 30