1

I have to ask you this because its driving me crazy since too many days.

I will explain my problem with an example:

s1="aa a"
s2="bbb"
s3="ccc"
s4="ddd"

v=($s1 $s2 $s3 $s4)
echo ${v[0]}
echo ${v[1]}
echo ${v[2]}
echo ${v[3]}
echo ${v[4]}

The output is:

aa
a
bbb
ccc
ddd

My problem is that v[0] contains "aa" and v[1] contains "a". I don't get why v[0] is not "aa a".

My output should be:

aa a
bbb
ccc 
ddd

...and v[4] should be empty

Why does the space create this problem?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Giuma79
  • 17
  • 5

1 Answers1

0

You need to add double quotes around your variables, like this:

v=("$s1" "$s2" "$s3" "$s4")

...so that you get:

v=("aa a" "bbb" "ccc" "ddd")

It is generally always a good idea to put your variables inside double quotes.


Without quotes you get the following:

v=(aa a bbb ccc ddd)

...so aa & a are interpreted as 2 separate strings.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56