I am trying to print the values of a variable which has naming sequentially in bash.
boxes1=abcd boxes2=efgh boxes3=ijkl
for (( i=1; i<=$boxCount; i++ ));
do
echo "\$$boxes{i}"
done
I want output like: abcd efgh ijkl
I am trying to print the values of a variable which has naming sequentially in bash.
boxes1=abcd boxes2=efgh boxes3=ijkl
for (( i=1; i<=$boxCount; i++ ));
do
echo "\$$boxes{i}"
done
I want output like: abcd efgh ijkl
declare -n
) or indirect refs (${!p}
) – ilkkachu Aug 04 '21 at 16:58for(( i=1; i<=3; i++ )); do declare -n var="boxes$i"; printf 'boxes%d = %s\n' "$i" "$var"; done -bash: declare: -n: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] boxes1 = -bash: declare: -n: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] boxes2 = -bash: declare: -n: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] boxes3 =
– sandeep yashodha Shiviraju Aug 04 '21 at 22:32boxes1 boxes2 boxes3
before you declare a nameref that references them. Alsofor (( i=1; i<=3; i++ )); do var=boxes$i; echo "${!var}"; done
. – Paul_Pedant Aug 05 '21 at 23:13