0

I am trying to read string into array using space as delimiter here I don't want to break down the spaces inside single quote or I can use double quote, if I use IFS='' (null), then all the words are getting read into first index but that's not purpose.

x="-r asdf -w test='foo      ' "
IFS=' '  read -r -a y<<<$x
echo ${y[@]}

Output: -r asdf -w test='foo ' #Spaces are gone.

Thanks.

  • So don't use `IFS=`. Read the whole line. And parse each character in the whole line manually. – KamilCuk Aug 27 '20 at 12:54
  • then whole sentence is reading into first index of variable. – sravan reddyboina Aug 27 '20 at 13:00
  • I do not understand your comment. What "sentence"? How a [sentence](https://www.oxfordlearnersdictionaries.com/definition/english/sentence_1) can do an action, ie. "read"? `I don't want to break down the spaces inside single quote or I can use double quote` Exactly - write your own parser that will handle that. – KamilCuk Aug 27 '20 at 13:01
  • Sorry for my wording, my point is the entire string was read into first element of array, If I remove IFS, > x="-r asdf -w test='foo '" > read -r -a y<<<$x > echo ${y[@]} -r asdf -w test='foo ' > echo ${y[0]} -r asdf -w test='foo ' > echo ${y[1]} – sravan reddyboina Aug 27 '20 at 13:19
  • you can't use quotes inside a string, they have no worth and treated as literally characters – alecxs Aug 27 '20 at 13:55
  • thats the purpose need those quotes – sravan reddyboina Aug 27 '20 at 14:40

0 Answers0