user4350786
user4350786

Reputation:

Bash Script Returning Value and Echo

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"

Upvotes: 0

Views: 63

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185025

#!/bin/bash

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

result=$(myfunc)   
echo "$result"

Upvotes: 1

Related Questions