A "name reference" variable is a variable that refers to another variable by name.
$ foo='hello world'
$ declare -n bar=foo
$ echo "$bar"
hello world
$ bar='goodbye'
$ echo "$foo"
goodbye
$ [[ -R foo ]] && echo nameref
$ [[ -R bar ]] && echo nameref
nameref
In the example above, the variable foo is ordinary while bar is a name reference variable, referring to the foo variable. Accessing the value $bar accesses $foo, and setting bar sets foo.
Name references are useful in various ways, for example when passing arrays to a function:
worker () {
local -n tasks="$1"
local -n hosts="$2"
for host in "${hosts[@]}"; do
for task in "${tasks[@]}"; do
# run "$task" on "$host"
done
done
}
mytasks=( x y z )
myhosts=( a b c )
my_otherhosts=( k l m )
worker mytasks myhosts # run my tasks on my hosts
worker mytasks my_otherhosts # run my tasks on my other hosts
The worker function above receives two strings as arguments. These strings are names of arrays. By using local with -n, I declare both tasks and hosts as name reference variables, referring to the named variables. As the variables whose name I pass to the function are arrays, I may use the name reference variables as arrays in the function.
The error in your code is that a name reference variable can not be an array, which is what your array1 variable is, which is why you get no as the output from your if statement. This error is also mentioned by the shell when you run your code:
$ bash script
Variable 'array1[10]' not exist
Variable 'array1[11]' is defined
Variable 'array1[12]' is defined
Variable 'array1[30]' not exist
Variable 'array1[50]' not exist
Variable 'array1[51]' not exist
Variable 'array1[128]' not exist
Variable 'array1[129]' not exist
Variable 'array1[130]' not exist
script: line 10: declare: array1: reference variable cannot be an array
triage
51=[trajectory]
129=[dynamic law]
no
You could have another variable refer to array1, though:
declare -n array2=array1