2

I'm trying to store a string with spaces into an array. I've used IFS="" and noticed that by doing this. my array_size is 1 although I have multiple strings. Is there a way to fix this?

the code i'm using

size=0
declare -a new
for t in ${temp};
do
  new[size++]=$t
done;
for n in ${new[@]};
do
  echo $n end
done;

my output is..

my string 1
my string 2
another string 3
another string 3 end

my desired output would like something like this..

 my string 1 end
 my string 2 end
 another string 3 end
user1709294
  • 1,675
  • 5
  • 18
  • 21

1 Answers1

3

To iterate over input with each item on separate line you have to set IFS to line feed.

You can do following to read the items to an array.

declare -a new
IFS=$'\n'
new=${temp}
hluk
  • 5,848
  • 2
  • 21
  • 19