0

I'm following the code mentioned here but it doesn't echo $result. Here is my code, I added quotations around the "$result". The echo in myfunc works, but the echo outside the function doesn't work. What is the problem? How do I fix it?

My Code:

#!/bin/bash

function myfunc()
{
    local myresult="Hello World"

}

result=$(myfunc)   
echo "$result"
6
  • which function is it called from? Is this the only small script? Commented Jan 10, 2015 at 15:56
  • @almasshaikh - Yes, it's the complete script. I'm just copying the code that was in the link I posted. Commented Jan 10, 2015 at 15:57
  • Returning Values from Bash Functions Commented Jan 10, 2015 at 15:58
  • @lurker - That is the link I posted. I got the code from there. Commented Jan 10, 2015 at 15:59
  • But it seems you mixed up their two examples. You were attempting to use half of one with half of the other. Commented Jan 10, 2015 at 16:00

1 Answer 1

1
#!/bin/bash

function myfunc()
{
    local myresult="Hello World"
    echo "$myresult" # the function need to return something
}

result=$(myfunc)   
echo "$result"
Sign up to request clarification or add additional context in comments.

4 Comments

Is the variable set in $result variable? Can I now use my $result
@CharlesWhitfield `` and $() are the same, will capture standard output of inner command and set it to a variable.
@CharlesWhitfield sure it does
Thanks - I thought the echo in the function wasn't needed. Guess I was wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.