3

I have a script myscript.sh

#!/bin/sh

echo $1 $2

which is used something like ...

./myscript.sh foo bar

which gives me an output of ...

foo bar

but how can i make this script include custom command options? for example ...

./myscript.sh -a foo and corespond to $1

and

./myscript.sh -b bar and corespond to $2
3
  • There's a great Q&A on StackOverflow that covers your options in some detail.
    – Seamus
    May 21, 2019 at 1:39
  • @Seamus now I know where they all get the useless quoting of the case variable and of the a:bc:optstring from (but do not care to quote arithm or other var expansions). They're cut&pasting from "great" Q&As. May 21, 2019 at 9:56
  • @UncleBilly: "There is nothing new under the sun" :)
    – Seamus
    May 21, 2019 at 10:01

2 Answers 2

4

You should to use man 1 bash:

Small example:

#!/bin/bash

while getopts "a:b:" opt      # get options for -a and -b ( ':' - option has an argument )
do
    case $opt in
        a) echo "Option a: $opt, argument: $OPTARG";;
        b) echo "Option b: $opt, argument: $OPTARG";;
    esac
done
4
  • A small example is like a picture, worth a thousand words! May 21, 2019 at 4:01
  • Assuming you can read code and it's got comments lol May 21, 2019 at 4:02
  • @roaima I've add man page about getopt (I hurried with answer) but example was about getopts. I omitted : also. That`s why I've been downvoted. I think so. Maybe the problem in something else. May 21, 2019 at 10:40
  • 1
    @YurijGoncharuk maybe because man 1 bash is not man 1 getopts ? That being said, I upvoted you! I also appreciated the man 1 bash link as it would do me very well to read it. So thanks my man! :)
    – Anonymous
    May 21, 2019 at 11:13
2

Since you are listing bash as the shell used, type:

$ help getopts

Which will print something like:

getopts: getopts optstring name [arg]
Parse option arguments.

Getopts is used by shell procedures to parse positional parameters as options.

OPTSTRING contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space.

Each time it is invoked, getopts will place the next option in the shell variable $name, initializing name if it does not exist, and the index of the next argument to be processed into the shell variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the shell variable OPTARG.

Getopts normally parses the positional parameters ($0 - $9), but if more arguments are given, they are parsed instead.

Related:

  1. Getopts tutorial.
  2. getopts How to pass command line options
  3. How to use getopts in bash
  4. Getopt, getopts or manual parsing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.