how to retrun a value from a function in shell script

fnPingFunction()
{
ping -c1 $1  
if [[ $? = 0 ]]
then result = 'Y'
else 
result = 'N'
fi 
} 
value=$(fnPingFunction "localhost")
echo $value

How do i get the value from the function ? value should be the result output Y or N

There is no need to return the value (and also functions return only integer values). In your POSIX-style function definition, the variables used are global (unless you define these in the function using typeset ). Just use the value of result after calling the function.

fnPingFunction "localhost"
value=$result
echo $value

or

fnPingFunction "localhost"
echo $result

directly.

Other ways:

foo () {
  local _fooresult='happy    end'
  echo "$_fooresult"
}

echo "$(foo)"
bar () {
    local _barresult=$1
    local _result='happy    end'
    eval $_barresult="'$_result'"
}

bar result
echo "$result"

--
Bye

I have changed the script as

fnPingFunction "localhost" echo $result

but it still shows me the same

Remove the spaces before and after the assignment operator = in your function. Also, put in a ; before that echo .

fnPingFunction()
{
ping -c1 $1 
if (( $? == 0 ))
then result='Y'
else 
result='N'
fi 
} 

And while calling, use

fnPingFunction localhost; echo $result