I have a difficulty getting over this man bash
passage.
If the control variable in a for loop has the
nameref attribute, the list of words can be a list of shell variables,
and a name reference will be established for each word in the list, in
turn, when the loop is executed. Array variables cannot be given the
-n attribute. However, nameref variables can reference array variables
and subscripted array variables.
Could you give an example of this nameref variable in a loop with some explanation?
Best Answer
A nameref variable is to a “normal” variable what a symbolic link is to a regular file.
A for loop executes the body with the loop variable (the “control variable”) bound in turn to each word in the list.
This is equivalent to writing out successive assignments:
If the loop variable is a nameref, then the body is executed with the nameref targeting each element of the word list in turn. This is not equivalent to a series of assignment like above: an assignment
ref=value
whereref
is a nameref would affect the variable thatref
is pointing to, but the for loop changes where the nameref points rather than following the reference to change the variable that it points to.The indirection can be observed through assignments as well, if you assign to the loop variable (which is uncommon, but permitted).
The last sentence explains that the target of a nameref can be an array. The nameref itself isn't an array, it's still a scalar variable, but when it's used in an assignment or in a dereference, it acts as the same type as the variable it points to.