I'm struggling to pass a multi-row array of floating-point numbers through a for loop, have it run a calculation, then define a new variable from the outputs.
Here's a simplified version of my Bash:
inputs=$(echo "12.12
34.34")
New_array=$( for var in "${inputs[@]}"; do
echo "${var}*2"| bc -l;
done )
I would expect the result from echo "$New_array" to be this:
$ echo "$New_array"
24.24
68.68
But I get this?
$ echo "$New_array"
12.12
68.68
Or whilst I've been troubleshooting (e.g. removing the $New_array variable):
(standard_in) 1: syntax error
I believe the problem has something to do with line return being read as an input for the first loop? But the solutions I've tried haven't worked so far.
Where am I going wrong?
bash scriptname.sh? I've only been debugging this in the terminal if that helps?inputsrequires it to be an actual array. See this question, and this one on the Unix&Linux SE. Do you actually want to use arrays (generally cleaner), or multiline strings? BTW,$(echo something)is almost always a mistake -- the$( )andechobasically cancel each other out.[@](i.e. use$inputsor${inputs}instead of${inputs[@]}). Also, beware that using unquoted variables will cause anything that looks like a filename wildcard to expand into a list of matching filenames, e.g. in2 * 3, the*will be replaced by a list of filenames in the current directory. This is one of the reasons that actual arrays are preferred.