1

it's my first question. I just want to know how could I save the result of the function 'square' into the variable 'sqr':

!/bin/bash

function square()
{
    let y=$x*$x
    return $y
}

x=3
**sqr=square**
echo "The square of $x is $sqr"
smxrlxp
  • 37
  • 8

1 Answers1

3

The functions in bash are really procedures (it does not return anything). Therefore you have two options: Save in a goblal variable the result, or save the output:

function myfunc(){
    myresult='anything'
}

myfunc
echo $myresult

or

function myfunc(){
    local   myresult='anything'
    echo "$myresult"
}

result=$(myfunc)
echo $result
smxrlxp
  • 37
  • 8
acruma
  • 318
  • 2
  • 17