If getopts
is a bash function, by my understanding, you need to pass $@
- the whole arguments to getopts
to let the function know what kind of arguments you have in order to proceed, right?
It seems to me that you don't need it, so how the getopts
get to know what arguments I have in the current scope? Does it have a way to trace the previous call like the other high-level languages do?
while getopts abcde opt; do
˄˄˄˄˄ <-- you only need to pass the argument labels here,
how getopts knows what arguments I have
case $opt in
...
esac
done
shift
andset
(well and.
,eval
(well andcommand
when called ascommand shift/eval...
)) are other shell builtins that can access/modify positional parameters.read
,readarray
,typeset
,unset
,[
,printf
are examples of shell builtins which likegetopts
can access/modify shell variables. – Stéphane Chazelas Nov 21 '23 at 14:41