- always test your code to https://shellcheck.net on errors (you have too much
do statements)
bash can't compute floating numbers itself, use bc [1] instead
- to do arithmetic substitution, use
$(( ))
- to do arithmetic, without arithmetic substitution, use
(( )) form
- UPPER CASE variables are reserved for system, better use lower case
inflation=(0 0.03 0.05) is an array, you can access it via "${inflation[@]}"
- quote variables ! [2]
[1] bc
bc <<< "scale=2; (4000*((1+0.07*(1-$r))/(1+$i))^10)"
[2]
Learn how to quote properly in shell, it's very important :
"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
Command Substitution: "$(cmd "foo bar")" causes the command 'cmd' to be executed with the argument 'foo bar' and "$(..)" will be replaced by the output. See http://mywiki.wooledge.org/BashFAQ/002 and http://mywiki.wooledge.org/CommandSubstitution
$((...)) is an arithmetic substitution. After doing the arithmetic, the whole thing is replaced by the value of the expression. See http://mywiki.wooledge.org/ArithmeticExpression
((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed.
Finally
#!/bin/bash
echo "Calculating the value v for all given values"
inflation=(0 0.03 0.05)
tax_rate=(0 0.28 0.35)
for i in "${inflation[@]}"; do
for r in "${tax_rate[@]}"; do
v="$(bc <<< "scale=2; (4000*((1+0.07*(1-$r))/(1+$i))^10)")"
echo -n "$v "
done
done
echo
Output
Calculating the value v for all given values
7840.00 6480.00 5920.00 5360.00 4400.00 4000.00 4400.00 4000.00 3600.00
((inside the assignment.V=(....)definesVas an array of words, as inV=( x y z ), and the for the parser, an unquoted(inside the list can't be handled. You could writeV=('4000*((1+0.07*(1-R))/(1+I))^10'), which would makeVa one-element array holding a string representation of this formula, but then I wonder why you need an array, if it has only one element. You could simply use a scalar:V='4000*((1+0.07*(1-R))/(1+I))^10'.