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

Bash getopts dropping last argument

So, I'm trying my hand at using bash's built-in getopts to handle argument processing except I'm getting a strange result. Here's my test script; #!/bin/sh HOST= OWNER= GROUP= while getopts "h:o:g" OPTION; do case $OPTION in h) …
Skittles
  • 2,866
  • 9
  • 31
  • 37
2
votes
4 answers

Bash script using getopts to store strings as an array

I am working on a Bash script that needs to take zero to multiple strings as an input but I am unsure how to do this because of the lack of a flag before the list. The script usage: script [ list ] [ -t ] [ -n ] The list takes zero,…
2
votes
2 answers

Optional arguments in Bash script

I am trying to write a function that has optional arguments in a way such that the optional arguments are follow by something like -x, for example my_function man_arg1 man_arg2 -n opt_arg1 -o opt_arg2 -x opt_arg3 and I want this to also support…
Sam
  • 23
  • 1
  • 4
2
votes
1 answer

Bash script with both parameter values and optional flags

I'm writing a bash script, which relies on a number of values provided via parameters and offers optional (boolean) flags. Based on the example on this page, I've extended the script, with the -h option, the to the following: human=false while…
2
votes
1 answer

Bash: Getopts doesn't run core loop ever

I was starting to understand getopts when it just stopped running all together. When I try getopts in small individual tests it works perfectly. I have been staring at my screen for a couple days now checking what's wrong, I even used a shellchecker…
Daemonique
  • 468
  • 2
  • 5
  • 15
2
votes
1 answer

Bash: default boolean value in getopts

I want to include a default option in my script where if the user uses this option, set the flag to true or it should be false by default. It seems that the script is not accepting false or true as boolean value. How can I make it…
Incognito
  • 135
  • 4
  • 14
2
votes
2 answers

Handling unused getopts argument

I have a script that starts with getopts and looks as follows: USAGE() { echo -e "Usage: bash $0 [-w ] [-o ] [-c ] [-t ] \n" 1>&2; exit 1; } if (($# == 0)) then USAGE fi while getopts ":w:o:c:t:h" opt do …
mf94
  • 439
  • 4
  • 19
2
votes
2 answers

Set specific option sets for arguments in bash

I've a script where I can pass different set of arguments for the work the script has to do. The options are specified below: [Show Help] ./myscript.sh -h [To Create] ./myscript.sh -c -f /input_loc/input_file.txt [To Export] ./myscript.sh -e -d…
Marcos
  • 845
  • 3
  • 10
  • 21
2
votes
1 answer

Issues Passing Multiple Mandatory Flags Using Getopts

I'm currently working on a script and I'm having a tough time wrapping my head around using multiple flags with mandatory arguments. For the sake of brevity, I have scaled it down to two options (even though its still pretty long) #THE NAME OF THE…
2
votes
1 answer

How to echo when there is no input in getopts command

This is put before the case statements. It still outputs nothing when executed without any input. while getopts :x:y:z: OPT; do if [ $OPT == "" ]; then echo "Null" exit 10 fi also, how should I code this to execute the values in any…
Kim
  • 23
  • 3
2
votes
0 answers

Using same getopts arguments for different options

I created a bash script and i need help with getopts Each options have arguments : while getopts ":a:b:c:" opt do case $opt in a) echo "$OPTARG";; b) echo "$OPTARG";; c) echo "$OPTARG";; /?) echo "wrong option $OPTARG";; esac done If i…
2
votes
1 answer

How to exit recent getopts options?

My script: run() { while getopts ":dr" option; do case "$option" in d) echo "__DEBUG__";; r) echo "__RELEASE__";; esac done if [ $option -eq ""] then echo "__DEBUG__" ; fi } Hi, i'm…
2
votes
1 answer

Explain ${!OPTIND} in getopts. Dangers? Alternatives?

I've been learning command line argument parsing. There are long threads about this already, I don't mean to provoke one here: Using getopts in bash shell script to get long and short command line options How do I parse command line arguments in…
pauljohn32
  • 2,079
  • 21
  • 28
2
votes
2 answers

In Bash, why does using getopts in a function only work once?

I'm trying to create a simple function on macOS Sierra that counts the characters in a string. This works fine (added to my bashrc file): function cchar() { str=$1 len=${#str} echo "string is $len char long" } $ cchar "foo" string is 3…
Trav Easton
  • 401
  • 2
  • 6
  • 17
2
votes
2 answers

Shell script with single choice options

I'm wondering, if it is possible to write a shell script with options using getopt or getopts that won't allow to combine multiple options? E.g., my script could be ran in two possible modes - long or short and with alpha or beta options, that's…