Create a function for handling inputs, then ordering won't matter.
What I would do is define global variables for your script using the typset command at the top of your script. Then I would handle the user input options using a function or just code it without a function. This way when input is not in order or it is missing it is properly handled.
The example below is using a case statement and it is using the build in shift option to go through all of the inputs. $1 is --option $2 is the value such as "/path/to/something", I have checks in there check if $2 is empty "-z" or || if it isn't, set it. When done items are either set or empty. In your code you will check if set or empty to determine if you are going to use that variable (not shown here.
# -- create globals --
typeset fooPath
typeset baz
typeset bar1
# -- get required commandline input --
get_user_input_options() {
while [[ $# -gt 0 ]] ;do
key="$1"
case ${key,,} in
--foo|--foo-path)
fooPath="${2}"
shift
;;
-b|--baz)
[[ -z "${2}" ]] || baz="${2}"
shift
;;
--bar1)
[[ -z "${2}" ]] || bar1="${2}"
shift
;;
*) echo "ERROR: Unknown option $key given."
exit 9
;;
esac
shift
done
}
# -- get inputs first in script logic --
get_user_input_options "$@"
echo $fooPath
echo $baz
echo $bar1
Example outputs:
[centos@ip-172-31-22-252 ~]$ ./t.sh --foo "/some/thing" --baz askdh
/some/thing
askdh
[centos@ip-172-31-22-252 ~]$ ./t.sh --foo "/some/thing" --baz askdh --bar1
/some/thing
askdh
[centos@ip-172-31-22-252 ~]$ ./t.sh --foo "/some/thing" --baz askdh --bar1 test
/some/thing
askdh
test
[centos@ip-172-31-22-252 ~]$ ./t.sh --foo "/some/thing" --baz askdh --bar1 test --notvalid askdha
ERROR: Unknown option --notvalid given.