bash-hackers-wiki/howto/getopts_tutorial/index.html

107 lines
69 KiB
HTML
Raw Normal View History

<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link href=https://flokoe.github.io/bash-hackers-wiki/howto/getopts_tutorial/ rel=canonical><link href=../edit-ed/ rel=prev><link href=../mutex/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.5.44"><title>Small getopts tutorial - The Bash Hackers Wiki</title><link rel=stylesheet href=../../assets/stylesheets/main.0253249f.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.06af60db.min.css><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=stylesheet href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback"><style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#small-getopts-tutorial class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="The Bash Hackers Wiki" class="md-header__button md-logo" aria-label="The Bash Hackers Wiki" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> The Bash Hackers Wiki </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Small getopts tutorial </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(pref
</code></pre></div> <p>These are all positional parameters, but they can be divided into several logical groups:</p> <ul> <li><code>-x</code> is an <strong>option</strong> (aka <strong>flag</strong> or <strong>switch</strong>). It consists of a dash (<code>-</code>) followed by <strong>one</strong> character.</li> <li><code>-f</code> is also an option, but this option has an associated <strong>option argument</strong> (an argument to the option <code>-f</code>): <code>/etc/mybackup.conf</code>. The option argument is usually the argument following the option itself, but that isn't mandatory. Joining the option and option argument into a single argument <code>-f/etc/mybackup.conf</code> is valid.</li> <li><code>-r</code> depends on the configuration. In this example, <code>-r</code> doesn't take arguments so it's a standalone option like <code>-x</code>.</li> <li><code>./foo.txt</code> and <code>./bar.txt</code> are remaining arguments without any associated options. These are often used as <strong>mass-arguments</strong>. For example, the filenames specified for <code>cp(1)</code>, or arguments that don't need an option to be recognized because of the intended behavior of the program. POSIX&reg; calls them <strong>operands</strong>.</li> </ul> <p>To give you an idea about why <code>getopts</code> is useful, The above command line is equivalent to:</p> <div class=highlight><pre><span></span><code>mybackup -xrf /etc/mybackup.conf ./foo.txt ./bar.txt
</code></pre></div> <p>which is complex to parse without the help of <code>getopts</code>.</p> <p>The option flags can be <strong>upper- and lowercase</strong> characters, or <strong>digits</strong>. It may recognize other characters, but that's not recommended (usability and maybe problems with special characters).</p> <h3 id=how-it-works>How it works<a class=headerlink href=#how-it-works title="Permanent link">&para;</a></h3> <p>In general you need to call <code>getopts</code> several times. Each time it will use the next positional parameter and a possible argument, if parsable, and provide it to you. <code>getopts</code> will not change the set of positional parameters. If you want to shift them, it must be done manually:</p> <div class=highlight><pre><span></span><code>shift $((OPTIND-1))
# now do something with $@
</code></pre></div> <p>Since <code>getopts</code> sets an exit status of <em>FALSE</em> when there's nothing left to parse, it's easy to use in a while-loop:</p> <div class=highlight><pre><span></span><code>while getopts ...; do
...
done
</code></pre></div> <p><code>getopts</code> will parse options and their possible arguments. It will stop parsing on the first non-option argument (a string that doesn't begin with a hyphen (<code>-</code>) that isn't an argument for any option in front of it). It will also stop parsing when it sees the <code>--</code> (double-hyphen), which means <a href=../../dict/end_of_options/ >end of options</a>.</p> <h3 id=used-variables>Used variables<a class=headerlink href=#used-variables title="Permanent link">&para;</a></h3> <table> <thead> <tr> <th>variable</th> <th>description</th> </tr> </thead> <tbody> <tr> <td><a href=../../syntax/shellvars/#OPTIND>OPTIND</a></td> <td>Holds the index to the next argument to be processed. This is how <code>getopts</code> "remembers" its own status between invocations. Also useful to shift the positional parameters after processing with <code>getopts</code>. <code>OPTIND</code> is initially set to 1, and <strong>needs to be re-set to 1 if you want to parse anything again with getopts</strong></td> </tr> <tr> <td><a href=../../syntax/shellvars/#OPTARG>OPTARG</a></td> <td>This variable is set to any argument for an option found by <code>getopts</code>. It also contains the option flag of an unknown option.</td> </tr> <tr> <td><a href=../../syntax/shellvars/#OPTERR>OPTERR</a></td> <td>(Values 0 or 1) Indicates if Bash should display error messages generated by the <code>getopts</code> builtin. The value is initialized to <strong>1</strong> on every shell startup - so be sure to always set it to <strong>0</strong> if you don't want to see annoying messages! <strong><code>OPTERR</code> is not specified by POSIX for the <code>getopts</code> builtin utility --- only for the C <code>getopt()</code> function in <code>unistd.h</code> (<code>opterr</code>).</strong> <code>OPTERR</code> is bash-specific and not supported by shells such as ksh93, mksh, zsh, or dash.</td> </tr> </tbody> </table> <p><code>getopts</code> also uses these variables for error reporting (they're set to value-combinations which arent possible in normal operation).</p> <h3 id=specify-what-you-want>Specify what you want<a class=headerlink href=#specify-what-you-want title="Permanent link">&para;</a></h3> <p>The base-syntax for <code>getopts</code> is:</p> <div class=highlight><pre><span></span><code>getopts OPTSTRING VARNAME [ARGS...]
</code></pre></div> <p>where:</p> <table> <thead> <tr> <th>Option</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>OPTSTRING</code></td> <td>tells <code>getopts</code> which options to expect and where to expect arguments (see below)</td> </tr> <tr> <td><code>VARNAME</code></td> <td>tells <code>getopts</code> which shell-variable to use for option reporting</td> </tr> <tr> <td><code>ARGS</code></td> <td>tells <code>getopts</code> to parse these optional words instead of the positional parameters</td> </tr> </tbody> </table> <h4 id=the-option-string>The option-string<a class=headerlink href=#the-option-string title="Permanent link">&para;</a></h4> <p>The option-string tells <code>getopts</code> which options to expect and which of them must have an argument. The syntax is very simple --- every option character is simply named as is, this example-string would tell <code>getopts</code> to look for <code>-f</code>, <code>-A</code> and <code>-x</code>:</p> <div class=highlight><pre><span></span><code>getopts fAx VARNAME
</code></pre></div> <p>When you want <code>getopts</code> to expect an argument for an option, just place a <code>:</code> (colon) after the proper option flag. If you want <code>-A</code> to expect an argument (i.e. to become <code>-A SOMETHING</code>) just do:</p> <div class=highlight><pre><span></span><code>getopts fA:x VARNAME
</code></pre></div> <p>If the <strong>very first character</strong> of the option-string is a <code>:</code> (colon), which would normally be nonsense because there's no option letter preceding it, <code>getopts</code> switches to "<strong>silent error reporting mode</strong>". In productive scripts, this is usually what you want because it allows you to handle errors yourself without being disturbed by annoying messages.</p> <h4 id=custom-arguments-to-parse>Custom arguments to parse<a class=headerlink href=#custom-arguments-to-parse title="Permanent link">&para;</a></h4> <p>The <code>getopts</code> utility parses the <a href=../../scripting/posparams/ >positional parameters</a> of the current shell or function by default (which means it parses <code>"$@"</code>).</p> <p>You can give your own set of arguments to the utility to parse. Whenever additional arguments are given after the <code>VARNAME</code> parameter, <code>getopts</code> doesn't try to parse the positional parameters, but these given words.</p> <p>This way, you are able to parse any option set you like, here for example from an array:</p> <div class=highlight><pre><span></span><code>while getopts :f:h opt &quot;${MY_OWN_SET[@]}&quot;; do
...
done
</code></pre></div> <p>A call to <code>getopts</code> <strong>without</strong> these additional arguments is <strong>equivalent</strong> to explicitly calling it with <code>"$@"</code>:</p> <div class=highlight><pre><span></span><code>getopts ... &quot;$@&quot;
</code></pre></div> <h3 id=error-reporting>Error Reporting<a class=headerlink href=#error-reporting title="Permanent link">&para;</a></h3> <p>Regarding error-reporting, there are two modes <code>getopts</code> can run in:</p> <ul> <li>verbose mode</li> <li>silent mode</li> </ul> <p>For productive scripts I recommend to use the silent mode, since everything looks more professional, when you don't see annoying standard messages. Also it's easier to handle, since the failure cases are indicated in an easier way.</p> <h4 id=verbose-mode>Verbose Mode<a class=headerlink href=#verbose-mode title="Permanent link">&para;</a></h4> <table> <thead> <tr> <th>failure</th> <th>message</th> </tr> </thead> <tbody> <tr> <td>invalid option</td> <td><code>VARNAME</code> is set to <code>?</code> (question-mark) and <code>OPTARG</code> is unset</td> </tr> <tr> <td>required argument not found</td> <td><code>VARNAME</code> is set to <code>?</code> (question-mark), <code>OPTARG</code> is unset and an <em>error message is printed</em></td> </tr> </tbody> </table> <h4 id=silent-mode>Silent Mode<a class=headerlink href=#silent-mode title="Permanent link">&para;</a></h4> <table> <thead> <tr> <th>failure</th> <th>message</th> </tr> </thead> <tbody> <tr> <td>invalid option</td> <td><code>VARNAME</code> is set to <code>?</code> (question-mark) and <code>OPTARG</code> is set to the (invalid) option character</td> </tr> <tr> <td>required argument not found</td> <td><code>VARNAME</code> is set to <code>:</code> (colon) and <code>OPTARG</code> contains the option-character in question</td> </tr> </tbody> </table> <h2 id=using-it>Using it<a class=headerlink href=#using-it title="Permanent link">&para;</a></h2> <h3 id=a-first-example>A first example<a class=headerlink href=#a-first-example title="Permanent link">&para;</a></h3> <p>Enough said - action!</p> <p>Let's play with a very simple case: only one option (<code>-a</code>) expected, without any arguments. Also we disable the <em>verbose error handling</em> by preceding the whole option string with a colon (<code>:</code>):</p> <div class=highlight><pre><span></span><code><a id=__codelineno-0-1 name=__codelineno-0-1 href=#__codelineno-0-1></a><span class=ch>#!/bin/bash</span>
<a id=__codelineno-0-2 name=__codelineno-0-2 href=#__codelineno-0-2></a>
<a id=__codelineno-0-3 name=__codelineno-0-3 href=#__codelineno-0-3></a><span class=k>while</span><span class=w> </span><span class=nb>getopts</span><span class=w> </span><span class=s2>&quot;:a&quot;</span><span class=w> </span>opt<span class=p>;</span><span class=w> </span><span class=k>do</span>
<a id=__codelineno-0-4 name=__codelineno-0-4 href=#__codelineno-0-4></a><span class=w> </span><span class=k>case</span><span class=w> </span><span class=nv>$opt</span><span class=w> </span><span class=k>in</span>
<a id=__codelineno-0-5 name=__codelineno-0-5 href=#__codelineno-0-5></a><span class=w> </span>a<span class=o>)</span>
<a id=__codelineno-0-6 name=__codelineno-0-6 href=#__codelineno-0-6></a><span class=w> </span><span class=nb>echo</span><span class=w> </span><span class=s2>&quot;-a was triggered!&quot;</span><span class=w> </span>&gt;<span class=p>&amp;</span><span class=m>2</span>
<a id=__codelineno-0-7 name=__codelineno-0-7 href=#__codelineno-0-7></a><span class=w> </span><span class=p>;;</span>
<a id=__codelineno-0-8 name=__codelineno-0-8 href=#__codelineno-0-8></a><span class=w> </span><span class=se>\?</span><span class=o>)</span>
<a id=__codelineno-0-9 name=__codelineno-0-9 href=#__codelineno-0-9></a><span class=w> </span><span class=nb>echo</span><span class=w> </span><span class=s2>&quot;Invalid option: -</span><span class=nv>$OPTARG</span><span class=s2>&quot;</span><span class=w> </span>&gt;<span class=p>&amp;</span><span class=m>2</span>
<a id=__codelineno-0-10 name=__codelineno-0-10 href=#__codelineno-0-10></a><span class=w> </span><span class=p>;;</span>
<a id=__codelineno-0-11 name=__codelineno-0-11 href=#__codelineno-0-11></a><span class=w> </span><span class=k>esac</span>
<a id=__codelineno-0-12 name=__codelineno-0-12 href=#__codelineno-0-12></a><span class=k>done</span>
</code></pre></div> <p>I put that into a file named <code>go_test.sh</code>, which is the name you'll see below in the examples.</p> <p>Let's do some tests:</p> <h4 id=calling-it-without-any-arguments>Calling it without any arguments<a class=headerlink href=#calling-it-without-any-arguments title="Permanent link">&para;</a></h4> <div class=highlight><pre><span></span><code>$ ./go_test.sh
$
</code></pre></div> <p>Nothing happened? Right. <code>getopts</code> didn't see any valid or invalid options (letters preceded by a dash), so it wasn't triggered.</p> <h4 id=calling-it-with-non-option-arguments>Calling it with non-option arguments<a class=headerlink href=#calling-it-with-non-option-arguments title="Permanent link">&para;</a></h4> <div class=highlight><pre><span></span><code>$ ./go_test.sh /etc/passwd
$
</code></pre></div> <p>Again --- nothing happened. The <strong>very same</strong> case: <code>getopts</code> didn't see any valid or invalid options (letters preceded by a dash), so it wasn't triggered.</p> <p>The arguments given to your script are of course accessible as <code>$1</code> - <code>${N}</code>.</p> <h4 id=calling-it-with-option-arguments>Calling it with option-arguments<a class=headerlink href=#calling-it-with-option-arguments title="Permanent link">&para;</a></h4> <p>Now let's trigger <code>getopts</code>: Provide options.</p> <p>First, an <strong>invalid</strong> one:</p> <div class=highlight><pre><span></span><code>$ ./go_test.sh -b
Invalid option: -b
$
</code></pre></div> <p>As expected, <code>getopts</code> didn't accept this option and acted like told above: It placed <code>?</code> into <code>$opt</code> and the invalid option character (<code>b</code>) into <code>$OPTARG</code>. With our <code>case</code> statement, we were able to detect this.</p> <p>Now, a <strong>valid</strong> one (<code>-a</code>):</p> <div class=highlight><pre><span></span><code>$ ./go_test.sh -a
-a was triggered!
$
</code></pre></div> <p>You see, the detection works perfectly. The <code>a</code> was put into the variable <code>$opt</code> for our case statement.</p> <p>Of course it's possible to <strong>mix valid and invalid</strong> options when calling:</p> <div class=highlight><pre><span></span><code>$ ./go_test.sh -a -x -b -c
-a was triggered!
Invalid option: -x
Invalid option: -b
Invalid option: -c
$
</code></pre></div> <p>Finally, it's of course possible, to give our option <strong>multiple times</strong>:</p> <div class=highlight><pre><span></span><code>$ ./go_test.sh -a -a -a -a
-a was triggered!
-a was triggered!
-a was triggered!
-a was triggered!
$
</code></pre></div> <p>The last examples lead us to some points you may consider:</p> <ul> <li><strong>invalid options don't stop the processing</strong>: If you want to stop the script, you have to do it yourself (<code>exit</code> in the right place)</li> <li><strong>multiple identical options are possible</strong>: If you want to disallow these, you have to check manually (e.g. by setting a variable or so)</li> </ul> <h3 id=an-option-with-argument>An option with argument<a class=headerlink href=#an-option-with-argument title="Permanent link">&para;</a></h3> <p>Let's extend our example from above. Just a little bit:</p> <ul> <li><code>-a</code> now takes an argument</li> <li>on an error, the parsing exits with <code>exit 1</code></li> </ul> <div class=highlight><pre><span></span><code><a id=__codelineno-1-1 name=__codelineno-1-1 href=#__codelineno-1-1></a><span class=ch>#!/bin/bash</span>
<a id=__codelineno-1-2 name=__codelineno-1-2 href=#__codelineno-1-2></a>
<a id=__codelineno-1-3 name=__codelineno-1-3 href=#__codelineno-1-3></a><span class=k>while</span><span class=w> </span><span class=nb>getopts</span><span class=w> </span><span class=s2>&quot;:a:&quot;</span><span class=w> </span>opt<span class=p>;</span><span class=w> </span><span class=k>do</span>
<a id=__codelineno-1-4 name=__codelineno-1-4 href=#__codelineno-1-4></a><span class=w> </span><span class=k>case</span><span class=w> </span><span class=nv>$opt</span><span class=w> </span><span class=k>in</span>
<a id=__codelineno-1-5 name=__codelineno-1-5 href=#__codelineno-1-5></a><span class=w> </span>a<span class=o>)</span>
<a id=__codelineno-1-6 name=__codelineno-1-6 href=#__codelineno-1-6></a><span class=w> </span><span class=nb>echo</span><span class=w> </span><span class=s2>&quot;-a was triggered, Parameter: </span><span class=nv>$OPTARG</span><span class=s2>&quot;</span><span class=w> </span>&gt;<span class=p>&amp;</span><span class=m>2</span>
<a id=__codelineno-1-7 name=__codelineno-1-7 href=#__codelineno-1-7></a><span class=w> </span><span class=p>;;</span>
<a id=__codelineno-1-8 name=__codelineno-1-8 href=#__codelineno-1-8></a><span class=w> </span><span class=se>\?</span><span class=o>)</span>
<a id=__codelineno-1-9 name=__codelineno-1-9 href=#__codelineno-1-9></a><span class=w> </span><span class=nb>echo</span><span class=w> </span><span class=s2>&quot;Invalid option: -</span><span class=nv>$OPTARG</span><span class=s2>&quot;</span><span class=w> </span>&gt;<span class=p>&amp;</span><span class=m>2</span>
<a id=__codelineno-1-10 name=__codelineno-1-10 href=#__codelineno-1-10></a><span class=w> </span><span class=nb>exit</span><span class=w> </span><span class=m>1</span>
<a id=__codelineno-1-11 name=__codelineno-1-11 href=#__codelineno-1-11></a><span class=w> </span><span class=p>;;</span>
<a id=__codelineno-1-12 name=__codelineno-1-12 href=#__codelineno-1-12></a><span class=w> </span>:<span class=o>)</span>
<a id=__codelineno-1-13 name=__codelineno-1-13 href=#__codelineno-1-13></a><span class=w> </span><span class=nb>echo</span><span class=w> </span><span class=s2>&quot;Option -</span><span class=nv>$OPTARG</span><span class=s2> requires an argument.&quot;</span><span class=w> </span>&gt;<span class=p>&amp;</span><span class=m>2</span>
<a id=__codelineno-1-14 name=__codelineno-1-14 href=#__codelineno-1-14></a><span class=w> </span><span class=nb>exit</span><span class=w> </span><span class=m>1</span>
<a id=__codelineno-1-15 name=__codelineno-1-15 href=#__codelineno-1-15></a><span class=w> </span><span class=p>;;</span>
<a id=__codelineno-1-16 name=__codelineno-1-16 href=#__codelineno-1-16></a><span class=w> </span><span class=k>esac</span>
<a id=__codelineno-1-17 name=__codelineno-1-17 href=#__codelineno-1-17></a><span class=k>done</span>
</code></pre></div> <p>Let's do the very same tests we did in the last example:</p> <h4 id=calling-it-without-any-arguments_1>Calling it without any arguments<a class=headerlink href=#calling-it-without-any-arguments_1 title="Permanent link">&para;</a></h4> <div class=highlight><pre><span></span><code>$ ./go_test.sh
$
</code></pre></div> <p>As above, nothing happened. It wasn't triggered.</p> <h4 id=calling-it-with-non-option-arguments_1>Calling it with non-option arguments<a class=headerlink href=#calling-it-with-non-option-arguments_1 title="Permanent link">&para;</a></h4> <div class=highlight><pre><span></span><code>$ ./go_test.sh /etc/passwd
$
</code></pre></div> <p>The <strong>very same</strong> case: It wasn't triggered.</p> <h4 id=calling-it-with-option-arguments_1>Calling it with option-arguments<a class=headerlink href=#calling-it-with-option-arguments_1 title="Permanent link">&para;</a></h4> <p><strong>Invalid</strong> option:</p> <div class=highlight><pre><span></span><code>$ ./go_test.sh -b
Invalid option: -b
$
</code></pre></div> <p>As expected, as above, <code>getopts</code> didn't accept this option and acted like programmed.</p> <p><strong>Valid</strong> option, but without the mandatory <strong>argument</strong>:</p> <div class=highlight><pre><span></span><code>$ ./go_test.sh -a
Option -a requires an argument.
$
</code></pre></div> <p>The option was okay, but there is an argument missing.</p> <p>Let's provide <strong>the argument</strong>:</p> <div class=highlight><pre><span></span><code>$ ./go_test.sh -a /etc/passwd
-a was triggered, Parameter: /etc/passwd
$
</code></pre></div> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li>Internal: <a href=../../scripting/posparams/ >posparams</a></li> <li>Internal: <a href=../../syntax/ccmd/case/ >case</a></li> <li>Internal: <a href=../../syntax/ccmd/while_loop/ >while_loop</a></li> <li>POSIX <a href=http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html#tag_20_54>getopts(1)</a> and <a href=http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html>getopt(3)</a></li> <li><a href=https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash>parse CLI ARGV</a></li> <li><a href=http://mywiki.wooledge.org/BashFAQ/035>handle command-line arguments (options) to a script</a></li> </ul> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">November 7, 2024</span> </span> <span class=md-source-file__fact> <span class=md-icon title=Created> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M14.47 15.08 11 13V7h1.5v5.25l3.08 1.83c-.41.28-.79.62-1.11 1m-1.39 4.84c-.36.05-.71.08-1.08.08-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8c0 .37-.03.72-.08 1.08.69.1 1.33.32 1.92.64.1-.56.16-1.13.16-1.72 0-5.5-4.5-10-10-10S2 6.5 2 12s4.47 10 10 10c.59 0 1.16-.06 1.72-.16-.32-.59-.54-1.23-.64-1.92M18 15v3h-3v2h3v3h2v-3h3v-2h-3v-3z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">November 7, 2024</span> </span> </aside> <h2 id=__comments>Comments</h2> <script src=https://giscus.app/client.js data-repo=flokoe/bash-hackers-wiki data-repo-id=R_kgDOJ3Nr6Q data-category="Giscus Page Comments" data-category-id=DIC_kwDOJ3Nr6c4CXq9t data-mapping=pathname data-strict=1 data-reactions-enabled=1 data-emit-metadata=0 data-input-position=top data-theme=preferred_color_scheme data-lang=en data-loading=lazy crossorigin=anonymous async>
</script> <script>
var giscus = document.querySelector("script[src*=giscus]")
/* Set palette on initial load */
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate" ? "dark" : "light"
giscus.setAttribute("data-theme", theme)
}
/* Register event handlers after documented loaded */
document.addEventListener("DOMContentLoaded", function() {
var ref = document.querySelector("[data-md-component=palette]")
ref.addEventListener("change", function() {
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate" ? "dark" : "light"
/* Instruct Giscus to change theme */
var frame = document.querySelector(".giscus-frame")
frame.contentWindow.postMessage(
{ giscus: { setConfig: { theme } } },
"https://giscus.app"
)
}
})
})
</script> </article> </div> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> Made with <a href=https://squidfunk.github.io/mkdocs-material/ target=_blank rel=noopener> Material for MkDocs </a> </div> </div> </div> </footer> </div> <div class=md-dialog data-md-component=dialog> <div class="md-dialog__inner md-typeset"></div> </div> <script id=__config type=application/json>{"base": "../..", "features": ["navigation.instant", "navigation.tracking", "navigation.tabs", "navigation.sections", "navigation.top", "content.action.view", "content.action.edit", "search.suggest", "search.highlight", "content.code.copy"], "search": "../../assets/javascripts/workers/search.6ce7567c.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script> <script src=../../assets/javascripts/bundle.83f73b43.min.js></script> </body> </html>