snippetbashTip
getopts — Parse shell options from arguments. Note: This command does not support longform options and thus us
Viewed 0 times
parseshellcommandgetoptsoptionsargumentsfromcli
Problem
How to use the
getopts command: Parse shell options from arguments. Note: 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>.Solution
getopts — Parse shell options from arguments. Note: 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>.Check if an option is the first set option in the current context:
getopts {{x}} {{opt}}; echo ${{opt}}Check if an option is set in a string (specified option must be the first element of the string):
getopts {{x}} {{opt}} "{{string text}}"; echo ${{opt}}Set an option to require an argument and print them:
getopts {{x}}: {{opt}}; echo ${{opt}} $OPTARGCheck for multiple options:
while getopts {{xyz}} {{opt}}; do case ${{opt}} in x) {{echo x is set}};; y) {{echo y is set}};; z) {{echo z is set}};; esac; doneSet
getopts to silent mode and handle option errors:while getopts :{{x:}} {{opt}}; do case ${{opt}} in x) ;; :) {{echo "Argument required"}};; ?) {{echo "Invalid argument"}} esac;; doneReset
getopts:OPTIND=1Code Snippets
Check if an option is the first set option in the current context
getopts {{x}} {{opt}}; echo ${{opt}}Check if an option is set in a string (specified option must be the first element of the string)
getopts {{x}} {{opt}} "{{string text}}"; echo ${{opt}}Set an option to require an argument and print them
getopts {{x}}: {{opt}}; echo ${{opt}} $OPTARGCheck for multiple options
while getopts {{xyz}} {{opt}}; do case ${{opt}} in x) {{echo x is set}};; y) {{echo y is set}};; z) {{echo z is set}};; esac; doneSet `getopts` to silent mode and handle option errors
while getopts :{{x:}} {{opt}}; do case ${{opt}} in x) ;; :) {{echo "Argument required"}};; ?) {{echo "Invalid argument"}} esac;; doneContext
tldr-pages: common/getopts
Revisions (0)
No revisions yet.