I do not find anything about the meaning of this:
case ${!i} in
--fa)
((i+=1))
fa=${!i}
;;
What is the meaning of
Starting an expansion with an exclamation point is indirect expansion described below.
If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of
${!prefix*}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
So if you have a declaration like i=foo
and another like foo=123
, you can reference the expansion of "foo" this way:
echo ${!i}
But if you just want to know what defined names start with "i":
$ iPad=123
$ i=foo
$ echo "${!i*}"
i iPad
And if you want to know what indices (or, in bash 4 associative arrays, keys) an array has:
$ i=(1 2)
$ i[9]=45
$ echo "${!i[@]}"
0 1 9
Note that indexed arrays in Bash are sparse!
The other part of your question is really a completely different question about the context of Arithmetic Evaluation. There are a handful of contexts where names are treated as integers.
"${foo[i++]}"
)(( i++ ))
)let
keyword. ( let j++
)integer
attribute. (declare -i
)The sigil ($
) is optional in arithmetic context, as long as it's not ambiguous. (It would be ambiguous to use $1
without the dollar sign.) In fact, you probably want to avoid it, given that (( $i++ ))
is a syntax error.
One really interesting side effect to declaring a name to have the integer attribute is that it implies a form of indirect expansion, distinct from the ${!name}
expression:
$ aname=123
$ anothername=aname
$ echo $anothername
aname
$ declare -i anothername
$ anothername=aname
$ echo $anothername
123
What's really going on here is that when anothername
is declared to be an integer, assignment expressions have arithmetic context on the right hand side. Even though aname
wasn't declared explicitly to be an integer, it is treated as one here.
$ anothername=aname++
$ echo $anothername $aname
123 124
For more information see ARITHMETIC EVALUATION in the bash manual.
case ${!i} in
--fa)
((i+=1))
fa=${!i}
;;
Let's say you're looping over options, and $3
expands to --fa
. But also, i=3
. This will cause "fa" to be set to the next option on the commandline ($4
), because i=4
by the time fa
is assigned.