I have an array named nmser in a bash script and I would like to make a string from it and store it in the variable flsel.

flsel=echo "${nmser[*]}"

1 Answer 1


If you drop the echo you’ll get the contents of the array, separated by spaces (strictly speaking, the first character of the IFS variable, which is space by default¹), in flsel:


¹ With IFS set to the empty string, array elements will be joined together without separator.

  • +1. and if they want it without spaces, they can use printf '%s'. e.g. flsel=$(printf '%s' "${nmser[@]}")
    – cas
    Jul 7, 2021 at 9:32
  • What is the difference between ${nmser[@]} and ${nmser[*]} ?
    – Pietru
    Jul 7, 2021 at 9:43
  • @Pietru inside double quotes, [@] expands to separate words (in shell terminology), which isn’t what you want here. See the Bash manual for details. Jul 7, 2021 at 9:44
  • Would I then have two options? flsel=${nmser[@]} and flsel="${nmser[*]}".
    – Pietru
    Jul 7, 2021 at 9:48
  • 1
    @Pietru No. flsel=${nmser[@]} is assigning a list to a single string, something that you semantically don't want to do (bash lets you do it, but that's just hiding an error of thought), while "${nmser[*]}" by definition is a string and what you should be using here.
    – Kusalananda
    Jul 7, 2021 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .