1

The post marked as duplicate above is similar, however is not sufficient for the use case. The below answers show a minimalist use of the read command to put parsed input for a known length of delimiter separated values into helpfully-named variables. For instance, if I read all four vars into $STATEMENTS,$BRANCHES,$FUNCTIONS,$LINES - a loop is not ideal as it adds a minimal of loop index awareness or 4 more lines to put each array var into a helpfully named var.

I have a list of comma separated numbers in a file:

26.16,6.89,23.82,26.17

I'd like to read these 4 numbers into helpfully named separate variable names - there will never fewer or more than 4 numbers.

Thanks for any help!

TheJeff
  • 3,665
  • 34
  • 52
  • The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself. – Cyrus Aug 12 '18 at 10:36
  • Follow up and separate question. Not a duplicate. Thanks for keeping tabs though @PesaThe – TheJeff Aug 12 '18 at 12:03
  • @TheJeff How is that not a duplicate...It's a questions that has been asked and answered here million times. – PesaThe Aug 12 '18 at 12:06
  • @PesaThe Writing it to output and parsing it into four variables are very different things. – TheJeff Aug 12 '18 at 12:19
  • @TheJeff Answers from the question I linked provide both. OP, just like you, wants the elements in variables or even better, in an array. Even the [accepted answer](https://stackoverflow.com/a/918931/6176817) uses an array. So not sure what your point is :) – PesaThe Aug 12 '18 at 12:35
  • @PesaThe I had looked at that post before posting this one. The answer linked to is for an indefinite number of parsed results, and isn't as elegant to get a scoped number of variables. The answers below use the read command to read 4 vars into a set of named variables, and don't include un-needed and un-wanted loops for arbitrary lengths of variables. Read my post again - you can see this fundamental difference, and a lack of this use of the read command in the linked post. These answers were helpful and different. Please re-consider your duplicate marking of this post. – TheJeff Aug 12 '18 at 21:27
  • @TheJeff If you look closely, you will find answers there that **use** variables instead of arrays. Although I still do think arrays are the better option. Consider that the format of your input string changes to `1,2,3,4,5,6`. The `d` variable will contain `4,5,6` whereas the array element `${x[3]}` will still correctly contain `4`. And the "unneeded" loops are there just to demonstrate a way to iterate over **all** the values. You can easily use them directly (`${x[0]}`, `${x[1]}`...). No matter how I look at it, this question is still an exact duplicate. – PesaThe Aug 12 '18 at 21:51
  • The point is that the d variable will never contain 6 items, thus the point of the post. Also, ${x[3]} is functionally equivalent, but comparisons of ${x[3]} are not as understandable. Surely you understand the value of code readability and elegance? Can you point me to where that answer uses the read command to put items into separately named variables? Because I don't think that is in there, and was the perfect answer for this occasion. – TheJeff Aug 12 '18 at 21:57
  • @TheJeff Look at this [answer](https://stackoverflow.com/a/3704011/6176817) for example. It also sets `IFS` only for the `read` command which is better that in the accepted answer here. – PesaThe Aug 12 '18 at 22:13
  • @PesaThe - If you still insist this is a dupe, that is a much better question to mark as duplicate, however I'll point out that the answer is 28 answers down, and the edited question shows a for loop as the answer. I think this guy is still a good trimmed down addition to this forum, but if you still don't think so, then please correct the dupe link and I can get back to my work. – TheJeff Aug 12 '18 at 22:15
  • It also might be nice to remove these comments from this thread, as I think they are distracting - but I'll defer to your judgement. – TheJeff Aug 12 '18 at 22:19

1 Answers1

0

You'll need read builtin. The input stream, and variables to read can vary, based on your personal preferences. For instance,

IFS=,
LIST=1,2,3,4
read a b c d <<<$LIST
echo $a ; echo $b ; echo $c ; echo $d
bipll
  • 11,747
  • 1
  • 18
  • 32