Skip to content

Instantly share code, notes, and snippets.

@juliyvchirkov
Created October 4, 2022 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliyvchirkov/dddbc9faea794bd5f85ed6c9f4c6e761 to your computer and use it in GitHub Desktop.
Save juliyvchirkov/dddbc9faea794bd5f85ed6c9f4c6e761 to your computer and use it in GitHub Desktop.
bash: long options parser
##
# long option format: --option=value
#
# usage: value="$(getOptionValue option "${@}")"
# script.sh --pidfile=/run/pidfile.pid ➙ pidfile="$(getOptionValue pidfile "${@}")"
##
getOptionValue() {
local option
[[ $# -gt 1 ]] || return
option="--${1}="
shift
while [[ $# -gt 0 ]]; do
[[ "${1:0:${#option}}" = "${option}" ]] && {
printf "%s" "${1:${#option}}"
return
}
shift
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment