2024-12-26 09:18:29 +02:00
|
|
|
# getopts
|
|
|
|
|
|
|
|
> Parse shell options from arguments.
|
|
|
|
> This command does not support longform options and thus using `getopt` is recommended instead.
|
|
|
|
> More information: <https://www.gnu.org/software/bash/manual/bash.html#index-getopts>.
|
|
|
|
|
$, case, continue, coproc, getopts, select, suspend, ulimit, while: refresh page (#17687)
Refreshed $, case, continue, coproc, getopts, select, suspend, ulimit, while .md pages
2025-08-16 21:28:41 +03:00
|
|
|
- Check if an option is the first set option in the current context:
|
2024-12-26 09:18:29 +02:00
|
|
|
|
$, case, continue, coproc, getopts, select, suspend, ulimit, while: refresh page (#17687)
Refreshed $, case, continue, coproc, getopts, select, suspend, ulimit, while .md pages
2025-08-16 21:28:41 +03:00
|
|
|
`getopts {{x}} {{opt}}; echo ${{opt}}`
|
2024-12-26 09:18:29 +02:00
|
|
|
|
$, case, continue, coproc, getopts, select, suspend, ulimit, while: refresh page (#17687)
Refreshed $, case, continue, coproc, getopts, select, suspend, ulimit, while .md pages
2025-08-16 21:28:41 +03:00
|
|
|
- Check if an option is set in a string (specified option must be the first element of the string):
|
2024-12-26 09:18:29 +02:00
|
|
|
|
$, case, continue, coproc, getopts, select, suspend, ulimit, while: refresh page (#17687)
Refreshed $, case, continue, coproc, getopts, select, suspend, ulimit, while .md pages
2025-08-16 21:28:41 +03:00
|
|
|
`getopts {{x}} {{opt}} "{{string text}}"; echo ${{opt}}`
|
|
|
|
|
|
|
|
- Set an option to require an argument and print them:
|
|
|
|
|
|
|
|
`getopts {{x}}: {{opt}}; echo ${{opt}} $OPTARG`
|
2024-12-26 09:18:29 +02:00
|
|
|
|
|
|
|
- Check for multiple options:
|
|
|
|
|
$, case, continue, coproc, getopts, select, suspend, ulimit, while: refresh page (#17687)
Refreshed $, case, continue, coproc, getopts, select, suspend, ulimit, while .md pages
2025-08-16 21:28:41 +03:00
|
|
|
`while getopts {{xyz}} {{opt}}; do case ${{opt}} in x) {{echo x is set}};; y) {{echo y is set}};; z) {{echo z is set}};; esac; done`
|
2024-12-26 09:18:29 +02:00
|
|
|
|
|
|
|
- Set `getopts` to silent mode and handle option errors:
|
|
|
|
|
$, case, continue, coproc, getopts, select, suspend, ulimit, while: refresh page (#17687)
Refreshed $, case, continue, coproc, getopts, select, suspend, ulimit, while .md pages
2025-08-16 21:28:41 +03:00
|
|
|
`while getopts :{{x:}} {{opt}}; do case ${{opt}} in x) ;; :) {{echo "Argument required"}};; ?) {{echo "Invalid argument"}} esac;; done`
|
2024-12-26 09:18:29 +02:00
|
|
|
|
|
|
|
- Reset `getopts`:
|
|
|
|
|
|
|
|
`OPTIND=1`
|