I know that in bash:
declare array=(hei{1,2}_{1,2})
will create an array with a list of elements:
echo ${array[*]}
hei1_1 hei1_2 hei2_1 hei2_2
but I would like to use variables in the array declaration,too:
var=1,2
declare array=(hei{$var}_{$var})
But it does not work:
echo ${array[*]}
hei{1,2}_{1,2}
Please help. I find very frustrating to specify something like hei{1,2}_{a,b,c}..n times..{hei,hei} in a code.
Thanks in advance
NOTE: It is possible in zsh without eval (got it from stackexchange.com): e.g., this script would do what i need, but in zsh (which I cannot always use):
#!/usr/bin/zsh
var1=( a c )
var2=( 1 2 )
arr=($^var1$^var2)
printf "${arr[*]}"
${array[*]}turns your array into a string, and since that string is unquoted, string-splits it and expands any globs within. Usingprintf '%q\n' "${array[@]}"is a safer, more accurate way to display the contents of an array, since it shows you the difference betweenarray=( "hello world" ),array=( hello world ), andarray=( hello $'world\r' ).