To assign to an array, put the elements in parentheses:
nodes=(node{1..3})
When using the array, you need to tell bash explicitly that you want to expand it as an array.
mycommand "${nodes[@]}"
Due to a design quirk of arrays in ksh and bash, $node
is the first element of the array, you need to explicitly ask for all the elements with the [@]
indexation. As always, you need double quotes around the variable expansion; with [@]
, each element is placed into a separate word.
See the bash manual for more information.
node=node{1..3}
doesn't work because brace expansion only happens in contexts that allow multiple words. An assignment to a scalar (string) variable only allows a single word. You also only get a single word when expanding the variable, since Bash does brace expansion before variable expansion.