Artificial intelligent assistant

passing in a different value for argument for recursive function in bash? say I have the following: n=10 function decrement { if [ $n -eq 0 ]; then echo recurse_done else echo $n decrement $(( $n-1 )) fi } decrement n above would create a infinite call to _decrement_ function and `n` would not decrement. I searched around and found `function $(( some_arithemtic_operation ))`, apparently it does not work..

You have to decide what argument you're passing to your function: is it a variable _name_ or a _value_?

If it's a name, indirect variable expansion is called for:


function decrement {
local var=$1
if [[ ${!var} -eq 0 ]]; then
echo recurse_done
else
echo ${!var}
declare "$var=$(( ${!var} - 1))"
decrement $var
fi
}
decrement n


If it's a value, your life is simpler


function decrement {
local value=$1
if [[ $value -eq 0 ]]; then
echo recurse done
else
echo $value
decrement $((value-1))
fi
}
decrement $n


But why is this recursive at all? `seq $n -1 1` will count down from $n to 1

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 2d4eaf0a05f5bc9929e42012ece644a1