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

1 Answer 1

4

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}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.