A template for option parsing in Bash based on getopt
This repository has been archived on 2021-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Franco Masotti 3fc977bc2a Minor fix. 2019-02-13 12:27:26 +01:00
LICENSE Imported and modified source code and documentation from blog post. 2018-12-02 18:23:54 +01:00
README.md Minor fix. 2019-02-13 12:27:26 +01:00
fbopt Minor fix. 2019-02-13 12:27:26 +01:00



A template for option parsing in Bash based on getopt

Table of contents


See https://frnmst.gitlab.io/notes/my-bash-option-parsing-template.html#reason



See all fbopt releases.


You can adapt and include the template as a separate part of your program by using source ./fbopt or . ./fbopt from the main script.

To allow inclusion in any project the template and its documentation is released under the CC0 1.0 license.


Bashisms are Bash specific syntax elements. I decided to use these because of programming convenience rather than compliancy.

You will find at least the following bashisms in the template

Bashism Example
local variables local variable='value'
arrays "${array[@]}"
indirect variables "${!variable}"

GNU Bash "strict mode"

This template enables the shell's "strict mode". You can disable it by commenting or removing the following line inside fbopt:

set -euo pipefail


The following elements should be common sense and not be specific to fbopt

Convention Notes Examples
all constants are enclosed within single quotes elements within single quotes (only) are not interpreted by the shell 'constant' or 'this is a constant'
some variables are enclosed within double quotes double quotes serve as a delimiter between multiple variable names if these are consecutive. Every variable between the quotes is interpolated "${variable}"
some variables are not enclosed within double quotes the only variables allowed without double quotes are integers (such as return values) and loop iterators (because these won't work otherwise) ${?} or for v in ${values}; do echo "${v}"; done
all variables use the curly braces notation curly braces serve as a delimiter between multiple variable names if these are consecutive "${variable}" or ${variable}
all variable names with a constant value must be capital case you might want to load some constants from a configuration file to override the values of options within the fbopt file local flag_a="${FLAG_A_DEFAULT_VALUE}"


The template is known to work with the the packages listed here and they must be installed on your system.

Package Executable Version command Package Version
GNU Bash /bin/bash $ bash --version GNU bash, version 4.4.23(1)-release (x86_64-unknown-linux-gnu) or GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
util-linux /bin/getopt $ getopt --version getopt from util-linux 2.33 or getopt from util-linux 2.27.1


  • An empty array results as unset causing fbopt to malfunction. This happens with GNU Bash 4.3.x but not with 4.4.x. A workaround is available for fbopt version 0.4.4 and later, in case fbopt is used with an older version of Bash. See also: https://mywiki.wooledge.org/BashFAQ/112#Negatives

Software using fbopt


Written in 2018 by Franco Masotti/frnmst franco.masotti@live.com

To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.

You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see http://creativecommons.org/publicdomain/zero/1.0/.