In the following code I dynamically generate variables based on variables names in an array. Then statically pint each referenced variable name in ref_names function.
#!/bin/bash
gen_names(){
for i in "${arr_vars[@]}"; do
file="${i##*/}"
file_no_ext="${file%%.*}"
declare -g ${file_no_ext}_exists=1
done
}
ref_names(){
echo dog=$dog_exists
echo cat=$cat_exists
echo moose=$moose_exists
}
main(){
var1="/etc/path/dog.txt"
var2="/etc/cat.doc"
var3="/moose.file"
arr_vars=(
$var1
$var2
$var3
)
gen_names
ref_names
}
main
However I want to be able to dynamically reference each dynamically generated name via loop of the same array in ref_names function instead of statically referencing each variable name as I am above. I tried the following but it does not work:
ref_names(){
for i in "${arr_vars[@]}"; do
file="${i##*/}"
file_no_ext="${file%%.*}"
echo ${${file_no_ext}_exists}
done
}
How can I dynamically reference each dynamically created name in bash? Also, this must be accomplished without using eval.