Creating a Bash array with elements that have whitespaces in them

Viewed 3900

I fail to understand what is happening between these two array creation, tested on cygwin GNU bash

$ array=('hello world' how are you)
$ echo $array
'hello world'    <----- This is expected   

EDIT : As chepner pointed it out the output is

hello world      <----- no ''

Now with variable assignment as an intermediate step

$ words="'hello world' how are you"
$ echo $words
'hello world' how are you
$ array=($words)
$ echo $array
'hello           <----- Why was it split with ' '? 
6 Answers

What about something like

$ array=($(echo -e $'a a\nb b' | tr ' ' '§')) && array=("${array[@]//§/ }") && echo "${array[@]/%/ INTERELEMENT}"

a a INTERELEMENT b b INTERELEMENT
Related