Celeb Glow
news | March 18, 2026

Handling bad values when parsing function arguments in bash

I want to handle bad values when parsing function arguments in bash. What can I do?

Bad values for -v (not 1,2 or 3) makes the function go wrong when parsing arguments.

myfunc -v 1eo -g -W 3

Here is the code

 while (( $# > 0 )); do opt="$1" case $opt in ("-v"|"--verbiage") if [[ $2 == [1-3] ]]; then vb="$2" ; shift ; shift else vb=1 ; shift 1 fi ;; ("-g") glob_ptrn=1 ; shift 1 ;; (*) break ;; esac done
4 Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy