Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Well, yeah: a=(1+2 3+4 5+6 7+8); for x in "${a[@]}"; do echo "Sum,C = $((C=$x))"; done. You almost never want to call out to some other process within a loop like that. And if it's not as simple as all that, you can still split out the array members with shell param expansions like for x in "${a[@]}"; do C=$((${x% *}+${x#* }))"; done. The awk stuff is way overkill. Commented Jul 2, 2015 at 11:37
  • @mikeserv Thanks. Point is that I don't always want to do sum. The sum is for a required simple demo. I can treat A and B separately to get something else, say C=A^2 and D=1-B^3. I was thinking of function statement where I can use A and B as argument $1 and $2. But I didn't try yet. :( Commented Jul 2, 2015 at 11:57
  • That's what I meant by suggesting the parameter expansions - if you set a delimiter you can divide on it. Try A=${x% *} B=${x#* }; echo "\$A=$A \$B=$B" to see what you get. Commented Jul 2, 2015 at 12:10
  • Yeah, that works, thanks. What do x% * and x#* do? Suppose, my set has three variables, i.e. {A,B,C}. Then could I have done in a similar fashion? Commented Jul 2, 2015 at 13:24
  • Yes, and no. They trim from the tail or head of a variable based on a pattern. Read this. Commented Jul 2, 2015 at 13:49