0

I need to add values into an array, but some values are separated with a whitespace. Then would be so fo example: "hello world"

myarr[0]: "hello 
myarr[1]: world"

How can I put it all together? For example:

myarr[0]: "hello world"
myarr[1]: "ciao mondo"
myarr[2]: ...
oguz ismail
  • 1
  • 16
  • 47
  • 69
Theo
  • 1
  • 3
    Does this answer your question? [Bash array with spaces in elements](https://stackoverflow.com/questions/9084257/bash-array-with-spaces-in-elements) – Freddy Mar 30 '20 at 08:49

1 Answers1

0

Use quotes around your strings with spaces :

ar+=("hello world" "ciao mondo")

echo ${ar[0]}
hello world

echo ${ar[1]}
ciao mondo

If you use the array in a loop, use quotes around the array:

for x in "${ar[@]}"; do echo "$x"; done
hello world
ciao mondo
mivk
  • 13,452
  • 5
  • 76
  • 69