I want to figure it out how recursion works in bash scripting.
I want to insert number as a parameter:
sh script.sh 4
And result would be (1+2+3+4)= 10
This is what I wrote and in my head works just fine, but can't make it work.
n=$1
j=1
result=0
recursion(){
result=`expr $result + $j`
j=`expr $j + 1`
if [ "$n" -gt 0 ]; then
recursion #is this how you do recursion?
n=`expr $n - 1
else
echo $result
fi
}
recursion
I think I imagined right but probably I am wrong.